杭电acm1213

2019-04-14 20:24发布

并查集 for(i=0; i//注意要-1,因为这里的i是从0开始
uni(buf[i].x-1, buf[i].y-1);
#include #define N 1001 struct d{ int x; int y; }buf[N]; int parent[N]; int find(int x) { return x==parent[x] ? x : find(parent[x]); } void uni(int x, int y) { int a = find(x); int b = find(y); if(a==b) return ; parent[a] = b; } int main() { int t, n, m, i, ans; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); for(i=0; i