hdu 1213 How Many Tables

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1213

How Many Tables

Description

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

Input

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

Sample Input

2
5 3
1 2
2 3
4 5

5 1
2 5

Sample Output

2
4

简单的并查集。。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::set;
15 using std::map;
16 using std::pair;
17 using std::vector;
18 using std::multiset;
19 using std::multimap;
20 #define pb(e) push_back(e)
21 #define sz(c) (int)(c).size()
22 #define mp(a, b) make_pair(a, b)
23 #define all(c) (c).begin(), (c).end()
24 #define iter(c) decltype((c).begin())
25 #define cls(arr,val) memset(arr,val,sizeof(arr))
26 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
27 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
28 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
29 const int Max_N = 1010;
30 typedef unsigned long long ull;
31 struct UnionFind {
32     bool exit[Max_N];
33     int par[Max_N], rank[Max_N];
34     inline void init(int n) {
35         rep(i, n + 1) {
36             par[i] = i;
37             rank[i] = 0;
38             exit[i] = false;
39         }
40     }
41     inline int find(int x) {
42         while (x != par[x]) {
43             x = par[x] = par[par[x]];
44         }
45         return x;
46     }
47     inline void unite(int x, int y) {
48         x = find(x), y = find(y);
49         if (x == y) return;
50         if (rank[x] < rank[y]) {
51             par[x] = y;
52             exit[x] = true;
53         } else {
54             par[y] = x;
55             exit[y] = true;
56             if (rank[x] == rank[y]) rank[x]++;
57         }
58     }
59     inline void result(int n) {
60         int tot = -1;
61         rep(i, n + 1) if (!exit[i]) tot++;
62         printf("%d
", tot);
63     }
64 }go;
65 int main() {
66 #ifdef LOCAL
67     freopen("in.txt", "r", stdin);
68     freopen("out.txt", "w+", stdout);
69 #endif
70     int t, n, m, a, b;
71     scanf("%d", &t);
72     while (t--) {
73         scanf("%d %d", &n, &m);
74         go.init(n);
75         rep(i, m) scanf("%d %d", &a, &b), go.unite(a, b);
76         go.result(n);
77     }
78     return 0;
79 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4607090.html