首页 > 其他 > 详细

【二分图最大匹配】P2055 [ZJOI2009]假期的宿舍

时间:2019-10-09 15:32:56      阅读:90      评论:0      收藏:0      [点我收藏+]
技术分享图片
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 
 5 int head[101];
 6 int cnt;
 7 
 8 struct Edge
 9 {
10     int u, next;
11 }e[10010];
12 
13 void add(int u, int v)
14 {
15     e[++cnt].u = v;
16     e[cnt].next = head[u];
17     head[u] = cnt;
18 }
19 
20 int n;
21 bool sc[51];
22 bool ho[51];
23 bool vis[51];
24 int match[51];
25 
26 bool dfs(int x)
27 {
28     for (int i = head[x]; i != -1; i = e[i].next)
29     {
30         if (!vis[e[i].u])
31         {
32             vis[e[i].u] = true;
33             if (!match[e[i].u] || dfs(match[e[i].u]))
34             {
35                 match[e[i].u] = x;
36                 return true;
37             }
38         }
39     }
40     return false;
41 }
42 
43 int T;
44 int tot;
45 
46 int main()
47 {
48     cin >> T;
49     while (T--)
50     {
51         memset(head, -1, sizeof(head));
52         cnt = 0;
53         tot = 0;
54         cin >> n;
55         for (int i = 1; i <= n; i++)
56         {
57             cin >> sc[i];
58         }
59         for (int i = 1; i <= n; i++)
60         {
61             cin >> ho[i];
62             if (!ho[i] && sc[i])
63             {
64                 add(i, i);
65             }
66         }
67         for (int i = 1; i <= n; i++)
68         {
69             if (!sc[i] || !ho[i] && sc[i]) tot++;
70         }
71         for (int i = 1; i <= n; ++i)
72         {
73             for (int j = 1; j <= n; ++j)
74             {
75                 int t;
76                 cin >> t;
77                 if (t && sc[j]) add(i, j);
78             }
79         }
80         memset(match, 0, sizeof(match));
81         int cnt = 0;
82         for (int i = 1; i <= n; i++)
83         {
84             if (sc[i] && !ho[i] || !sc[i])
85             {
86                 memset(vis, false, sizeof(vis));
87                 if (dfs(i)) cnt++;
88             }
89         }
90         if (cnt == tot) cout << "^_^" << endl;
91         else cout << "T_T" << endl;
92     }
93 }
View Code

 

【二分图最大匹配】P2055 [ZJOI2009]假期的宿舍

原文:https://www.cnblogs.com/thjkhdf12/p/11641319.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!