poj2239(二分图)

来源:互联网 发布:万能五笔有mac版吗 编辑:程序博客网 时间:2024/04/30 07:27

题意:不多写了,又是裸的二分图匹配;

解法:写这道题时,第一次用邻接表建图写了匈牙利。而且还从neko那里学了一招,就是匈牙利的used函数在for循环里可以不用memset,用一个ncase代替就可以,降低了很多时间复杂度,瞬间觉得又学了的一招。

代码:

/***************************************************** author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>using namespace std;#define eps 1e-8typedef long long LL;int used[110];int match[310];bool num[310][100];struct edge{    int v;    int next;} edges[310000];int head[31000];int count1=0;int ncase=0;void addedge(int u,int v){    edges[count1].v=v;    edges[count1].next=head[u];    head[u]=count1++;}int n;bool dfs(int k){    for(int i=head[k];i!=-1;i=edges[i].next)    {        int t=edges[i].v;        if(used[t]!=ncase)        {            used[t]=ncase;            if(match[t]==-1||dfs(match[t]))            {                match[t]=k;                return true;            }        }    }    return false;}int main(){   while(cin>>n)   {       count1=0;       memset(head,-1,sizeof head);       memset(match,-1,sizeof match);       for(int i=0;i<n;i++)       {           int t;scanf("%d",&t);           for(int j=0;j<t;j++){               int a,b;scanf("%d%d",&a,&b);               addedge(i+1,(a-1)*12+b);           }       }       int ans=0;       for(int i=1;i<=n;i++)       {           ncase++;           if(dfs(i)) ans++;       }       cout<<ans<<'\n';   }   return 0;}

0 0
原创粉丝点击