POJ 1469 二分图最大匹配

思路:裸的二分图最大匹配,居然没看出来,我还是要学习一个

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<sstream>
 8 #include<iomanip>
 9 using namespace std;
10 const int MOD = 1e9 + 7;
11 
12 typedef long long LL;
13 typedef unsigned long long ULL;
14 
15 const int maxn = 300 + 5;
16 bool g[maxn][maxn];
17 int link[maxn];
18 bool check[maxn];
19 
20 int n,m,k;
21 bool dfs(int u){
22    for(int i = 1; i <= m; ++i){
23        if(!check[i] && g[u][i]){
24            check[i] = true;
25            if(link[i] == -1 || dfs(link[i])){
26                 link[i] = u;
27                 return true;
28            }
29        }
30    }
31    return false;
32 }
33 
34 int hugarian(){
35    int ans = 0;
36    memset(link,-1,sizeof(link));
37    for(int i = 1; i <= n; ++i){
38       memset(check,0,sizeof(check));
39       if(dfs(i))
40          ++ans;
41    }
42    return ans ;
43 }
44 
45 int main(){
46    int t;
47    scanf("%d",&t);
48    while(t--){
49        memset(g,0,sizeof(g));
50        scanf("%d%d",&n,&m);
51        for(int i = 1; i <= n; ++i){
52           int d;
53           scanf("%d",&d);
54           for(int j = 0; j < d; ++j){
55               int x;
56               scanf("%d",&x);
57               g[i][x] = 1;
58           }
59        }
60        int num = hugarian();
61        if(num == n){
62            printf("YES\n");
63        }
64        else printf("NO\n");
65    }
66    return 0;
67 }

 

posted @ 2016-08-10 15:42  PosProteus  阅读(188)  评论(0编辑  收藏  举报