uva 10608(并查集)
题意就不描述了,最简单的并查集。秒掉就OK
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <stack> 8 #include <utility> 9 #define ll long long 10 #define LEN 1000100 11 #define INF 0x7fffffff 12 13 using namespace std; 14 15 int n, m; 16 int parent[LEN]; 17 18 void init() 19 { 20 for(int i=0; i<LEN; i++){ 21 parent[i] = -1; 22 } 23 } 24 25 int Find(int x){ 26 return parent[x]<0?x:Find(parent[x]); 27 } 28 29 void Union(int a, int b){ 30 int pa = Find(a), pb = Find(b); 31 if(pa!=pb){ 32 parent[pb] += parent[pa]; 33 parent[pa] = pb; 34 } 35 } 36 37 int main() 38 { 39 // freopen("in.txt", "r", stdin); 40 41 int T, a, b; 42 scanf("%d", &T); 43 while(T--){ 44 init(); 45 scanf("%d%d", &n, &m); 46 for(int i=0; i<m ;i++){ 47 scanf("%d%d", &a, &b); 48 Union(a, b); 49 } 50 int ans = 0; 51 for(int i=1; i<=n; i++){ 52 ans = min(ans, parent[i]); 53 } 54 cout << -ans << endl; 55 } 56 return 0; 57 }
奔跑吧!少年!趁着你还年轻