poj 1611

这道题主要是为了记录自己把这道题想复杂了,一开始都用上了set

其实很简单,每一组的归到一起,然后统计与0不是一个根节点的点有多少

合理性的话是基于并查集每次合并都是从根上合并!

#include <iostream>
#include <cstdio>
#include <set>
using namespace std;

const int N = 30100;

int pre[N];

int find(int x){
	int r = x;
	while(r != pre[r])
		r = pre[r];
	int i = x, j;
	while(i != r){
		j = pre[i];
		pre[i] = r;
		i = j;
	}
	return r;
}

void join(int x, int y){
	int f1 = find(x), f2 = find(y);
	if(f1 != f2)
		pre[f2] = f1;
}

int main(){
	int n, m;
	while(cin >> n >> m, n || m){
		int a[N];
		int ans = 0;
		for(int i = 0; i < n; i ++)
			pre[i] = i;
		while(m --){
			int k;
			cin >> k;
			for(int i = 0; i < k; i ++){
				cin >> a[i];
			}
			for(int i = 1; i < k; i ++){
				join(a[0], a[i]);
			}
		}
		int x = find(0);
		for(int i = 0; i < n; i ++){
			if(find(i) == x)
				ans ++;
		}
		cout << ans << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38759433/article/details/80170492