标签:des blog class code ext color
Description
Input
Output
Sample Input
Sample Output
Hint
Hint Huge input, scanf is recommended.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 |
#include <iostream>#include <stdio.h>#include <string.h>using
namespace std;const
int MAXX = 1000;int
father[MAXX];bool
vis[MAXX];int
find(int
x){ return
x==father[x]?x:find(father[x]);}bool
unionsearch(int
a,int
b){ int
fa = find (a); int
fb = find (b); if(fa==fb) return
false; else { father[fb] = fa; return
true; }}int
main(){ int
n,m; while(scanf("%d",&n)&&n) { scanf("%d",&m); int
ans = 0; memset(father,0,sizeof(father)); memset(vis,false,sizeof(vis)); for(int
i=1;i<=n;i++) { father[i] = i; } for(int
i =1;i<=m;i++ ) { int
a,b; scanf("%d %d",&a,&b); if(unionsearch(a,b)) { vis[b] = vis[a] = true; } } for(int
i=1;i<=n;i++) { if(father[i]==i) ans++; } cout<<ans-1<<endl; } return
0;} |
标签:des blog class code ext color
原文地址:http://www.cnblogs.com/locojyw/p/3716073.html