标签:hdu 1083 courses 二分图匹配 匈牙利算法
2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1
YES NO
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define N 333
using namespace std;
int n,p;
vector<int>G[N];
int linker[N];
bool used[N];
bool dfs(int u) {
int v;
for(v=0; v<G[u].size(); v++) {
int i=G[u][v];
if(!used[i]) {
used[i]=true;
if(linker[i]==-1||dfs(linker[i])) {
linker[i]=u;
return true;
}
}
}
return false;
}
int hungary() {
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=1; u<=p; u++) {
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
int main() {
// freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--) {
for(int i=0; i<N; i++)G[i].clear();
scanf("%d%d",&p,&n);
for(int i=1; i<=p; i++) {
int num,x;
scanf("%d",&num);
while(num--) {
scanf("%d",&x);
G[i].push_back(x);
}
}
int res=hungary();
if(res<p)printf("NO\n");
else printf("YES\n");
}
return 0;
}标签:hdu 1083 courses 二分图匹配 匈牙利算法
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/45727991