hdu--1231--并查集连分量的个数
生活随笔
收集整理的這篇文章主要介紹了
hdu--1231--并查集连分量的个数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我覺得 這題 是純粹的 并查集 可以算成 入門題吧
問你有幾章桌子 就是問你有幾個 連通塊嘛 一個道理
touch? me
這題 我采用了下 father[x]開始 初始化為-1
1 #include <iostream> 2 #include <cstring> 3 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 ; i<=n ; 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
總結
以上是生活随笔為你收集整理的hdu--1231--并查集连分量的个数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新浪微博2-找到用户的accessTok
- 下一篇: Django学习笔记2:一个简单的开发实