POJ 1469 二分图最大匹配

思路:裸的二分图最大匹配

 1 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 #include
 8 #include
 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 }

 

你可能感兴趣的:(二分图)