标签:
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4175 Accepted Submission(s): 1990
/** 题意:n个student,m个course,然后每个course选一个课代表,然后看能否为每门course 选到一名课代表 做法:匈牙利算法 **/ #include<iostream> #include<stdio.h> #include<cmath> #include<string.h> using namespace std; #define maxn 310 int linker[maxn]; int un,vn; int g[maxn][maxn]; bool used[maxn]; int n,m; int dfs(int u) { for(int i=1;i<=m;i++) { if(g[u][i] && used[i] == false) { used[i] = true; if(linker[i] == -1 || dfs(linker[i])) { linker[i] = u; return 1; } } } return 0; } int hungary() { int res = 0; memset(linker,-1,sizeof(linker)); for(int i=0;i<n;i++) { memset(used,false,sizeof(used)); res += dfs(i); } return res; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE int T; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); int Q; memset(g,0,sizeof(g)); int v; for(int i=0;i<n;i++) { scanf("%d",&Q); while(Q--) { scanf("%d",&v); g[i][v] = 1; } } int res = 0; res = hungary(); if(res == n) printf("YES\n"); else printf("NO\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4392434.html