题目:http://acm.hdu.edu.cn/showproblem.php?pid=2063
又是一道二分图匹配的裸题,直接上匈牙利算法
注意一点它末尾的0结束,是标志着有多组数据……坑……
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<ctime> 8 #include<queue> 9 #define fre(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout) 10 #define Test1 freopen("in.in","r",stdin);freopen("1.out","w",stdout) 11 #define Test2 freopen("in.in","r",stdin);freopen("2.out","w",stdout) 12 using namespace std; 13 typedef long long LL; 14 typedef double db; 15 const double CPS=CLOCKS_PER_SEC,TL=0.98; 16 const int oo=2147483647,N=501,M=1001; 17 int first[N],Next[M],v[M],g[N]; 18 bool f[N]; 19 inline bool xyl(int x) 20 { 21 int i,k; 22 for (i=first[x];i;i=Next[i]) 23 { 24 k=v[i]; 25 if (f[k]) 26 { 27 f[k]=0; 28 if ((!g[k])||xyl(g[k])) 29 { 30 g[k]=x; 31 return 1; 32 } 33 } 34 } 35 return 0; 36 } 37 int main() 38 { 39 int n,na,nb,i,j,ans,x; 40 scanf("%d",&n); 41 while (n) 42 { 43 ans=0; 44 scanf("%d%d",&na,&nb); 45 for (i=1;i<=na;i++) first[i]=0; 46 for (i=1;i<=nb;i++) g[i]=0; 47 for (i=1;i<=n;i++) 48 { 49 scanf("%d%d",&x,&v[i]); 50 Next[i]=first[x]; 51 first[x]=i; 52 } 53 for (i=1;i<=na;i++) 54 { 55 for (j=1;j<=nb;j++) f[j]=1; 56 if (xyl(i)) ans++; 57 } 58 printf("%d ",ans); 59 scanf("%d",&n); 60 } 61 return 0; 62 }
版权所有,转载请联系作者,违者必究