通信公司网站建设,网站建设与管理职责,如何制作一个软件,wordpress page 2我觉得 这题 是纯粹的 并查集 可以算成 入门题吧 问你有几章桌子 就是问你有几个 连通块嘛 一个道理 touch me 这题 我采用了下 father[x]开始 初始化为-1 1 #include iostream2 #include cstring3 using namespace std;4 5 const int size 1010;6 int fathe…我觉得 这题 是纯粹的 并查集 可以算成 入门题吧 问你有几章桌子 就是问你有几个 连通块嘛 一个道理 touch me 这题 我采用了下 father[x]开始 初始化为-1 1 #include iostream2 #include cstring3 using namespace std;4 5 const int size 1010;6 int father[size];7 8 int find( int x )9 {
10 return father[x] -1 ? x : father[x] find( father[x] );
11 }
12
13 void Union( int x , int y )
14 {
15 x find(x);
16 y find(y);
17 if( x!y )
18 {
19 father[x] y;
20 }
21 }
22
23 int main()
24 {
25 int t , n , m , cnt , x , y;
26 cin t;
27 while( t-- )
28 {
29 cnt 0;
30 cin n m;
31 memset( father , -1 , sizeof(father) );
32 while( m-- )
33 {
34 cin x y;
35 Union( x, y );
36 }
37 for( int i 1 ; in ; i )
38 {
39 if( father[i] -1 )
40 {
41 cnt ;
42 }
43 }
44 cout cnt endl;
45 }
46 return 0;
47 } View Code 转载于:https://www.cnblogs.com/radical/p/3908688.html