UVA 10608
并查集的水題,比賽時怎么都AC不了,后來發現是沒有考慮先3 2 后1 2的情況,下次注意!
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int x[30010]; int k[30010]; int num[30010]; int find(int y) {int r= y;while(x[r]!= r){r= x[r];}/*int i = y;while(i!= r){int j = x[i];x[i] = r;i = j;}*/return r; }void merge(int a,int b) {a= find(a);b= find(b);if(a!= b){x[a] = b;num[a]+=num[b];num[b]=num[a];}}int main() {int n ;cin >> n;while(n--){for(int i =0; i< 30001;i++){k[i] = 0;num[i] = 1;}int a,b;cin >>a>> b;for(int i =1; i<= a;i++){x[i] = i;}int p,q;for(int i =0;i< b;i++){cin >> p>> q;merge(p,q);}int max = 0;//cout<< num[2];for(int i =1; i<30010;i++){if(num[i] > max){max = num[i];//cout<<num[i];}}cout<< max<< endl;}return 0; }
轉載于:https://www.cnblogs.com/DUANZ/p/3871455.html
總結
- 上一篇: C1 FlexGrid 行Style设置
- 下一篇: 推荐13个CSS3快速开发工具