【HDUOJ】1213 How many tables

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213

题意:Ignatius邀请了n个朋友来家里,朋友之间如果互相不认识的不想坐一起,所以至少要准备几张桌子。

题解:啊。裸题。直接输入join一下,然后最后统计同父亲有多少个就行。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int maxn = 1e6+10;
 5 
 6 int f[maxn];
 7 
 8 void init(int n){
 9     for(int i = 0 ; i <= n ;i++){
10         f[i] = i;
11     }
12 }
13 int find(int x){
14     if( x != f[x]){
15         f[x] = find(f[x]);
16     }
17     return f[x];
18 }
19 void join(int a,int b){
20     int x = find(a);
21     int y = find(b);
22     if(x!=y)
23        f[x]=y;
24 }
25 
26 bool judge(int x,int y){
27    x=find(x);
28    y=find(y);
29    if(x!=y)    return true;
30    return false;
31 }
32 
33 int n,m;
34 int main(){
35     int T;
36     cin>>T;
37     while(T--){
38         int cnt = 0;
39         cin>>n>>m;
40         init(n);
41         int a,b;
42         for(int i = 0; i < m ;i++){
43             cin>>a>>b;
44             join(a,b);
45         }
46         for(int i = 1; i <= n ;i++){
47             if(f[i] == i){
48                 cnt++;
49             }
50         }
51         cout<<cnt<<endl;
52     }
53 
54     return 0;
55 }

猜你喜欢

转载自www.cnblogs.com/Asumi/p/9747659.html
今日推荐