标签:
1 /*
2 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no
3 每一次二分图匹配时,将点多的集合加大最后第一个集合去
4 注意:n <= 1,no,两个集合都至少有一人;ans == n,扔一个给另一个集合
5 */
6 #include <cstdio>
7 #include <algorithm>
8 #include <cstring>
9 #include <cmath>
10 #include <string>
11 #include <vector>
12 #include <map>
13 using namespace std;
14
15 const int MAXN = 1e5 + 10;
16 const int INF = 0x3f3f3f3f;
17 vector<int> G[MAXN];
18 int col[MAXN];
19 int cnt[3];
20 int n, m;
21
22 bool DFS(int u)
23 {
24 for (int i=0; i<G[u].size (); ++i)
25 {
26 int v = G[u][i];
27 if (col[v] == -1) {
28 col[v] = 3 - col[u]; cnt[col[v]]++;
29 if (!DFS (v)) return false;
30 }
31 else if (col[v] == col[u]) return false;
32 }
33
34 return true;
35 }
36
37 void work(void)
38 {
39 memset (col, -1, sizeof (col));
40 int ans = 0;
41 for (int i=1; i<=n; ++i)
42 {
43 if (col[i] == -1)
44 {
45 col[i] = 1; cnt[1] = 1; cnt[2] = 0;
46 if (!DFS (i)) {
47 puts ("Poor wyh"); return ;
48 }
49 ans += max (cnt[1], cnt[2]);
50 }
51 }
52 if (ans == n) ans--;
53 printf ("%d %d\n", ans, n - ans);
54 }
55
56 int main(void) //BestCoder Round #48 ($) 1002 wyh2000 and pupil
57 {
58 int t; scanf ("%d", &t);
59 while (t--)
60 {
61 scanf ("%d%d", &n, &m);
62 if (n <= 1) {
63 puts ("Poor wyh"); continue;
64 }
65 for (int i=1; i<=n; ++i) G[i].clear ();
66 for (int i=1; i<=m; ++i)
67 {
68 int u, v; scanf ("%d%d", &u, &v);
69 G[u].push_back (v); G[v].push_back (u);
70 }
71 work ();
72 }
73
74
75 return 0;
76 }
二分图判定+点染色 BestCoder Round #48 ($) 1002 wyh2000 and pupil
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4658530.html