标签:之间 targe 工程 ret 12px iss 通过 show 正整数
题目:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 65783 Accepted Submission(s): 35114
#include <iostream> int Find(int a[], int x) { while(x != a[x]) x = a[x]; return x; } bool IsConnected(int a[], int x, int y) { return Find(a,x) == Find(a,y); } void Union(int a[], int x, int y, int& num) { int r1 = Find(a,x); int r2 = Find(a,y); a[r1] = r2; num--; } int main() { using namespace std; int N=1, M; while(1) { cin >> N; if(N == 0) return 0; cin >> M; int * a = new int[N+1]; for(int i = 0; i < N+1; i++) a[i] = i; int num = N-1; int x, y; while(M--) { cin >> x >> y; if(IsConnected(a,x,y)) continue; Union(a,x,y,num); } printf("%d\n", num); delete[] a; } }
标签:之间 targe 工程 ret 12px iss 通过 show 正整数
原文地址:https://www.cnblogs.com/w-j-c/p/9218922.html