poj 1469 匈牙利最大匹配 模板

来源:互联网 发布:java培训出来好找工作? 编辑:程序博客网 时间:2024/05/02 04:41
#include<cstdio>#include<iostream>#include<cstring>#include<vector>using namespace std;int P,N;int visit[330];int tot;vector<int> coll[110];int from[330];bool match(int x){   for(int i=0;i<coll[x].size();++i)   if(!visit[coll[x][i]]){      visit[coll[x][i]]=true;      if(from[coll[x][i]]==-1||match(from[coll[x][i]])){        from[coll[x][i]]=x;        return true;      }   }   return false;}int hungary(){   tot=0;   memset(from,255,sizeof from);   for(int i=1;i<=P;++i){     memset(visit,0,sizeof visit);     if(match(i))       ++tot;   }   return tot;}int main(){  int T;  cin>>T;  while(T--){     scanf("%d%d",&P,&N);     for(int i=1;i<=P;i++){       int m;       scanf("%d",&m);//记着都用scanf,否则用cin会TLE       for(int j=0;j<m;j++){         int a;         scanf("%d",&a);         coll[i].push_back(a);       }     }     int ans=hungary();     if(ans==P)        printf("YES\n");     else        printf("NO\n");     for(int i=1;i<=P;i++)        coll[i].clear();  }}

原创粉丝点击