其他分享
首页 > 其他分享> > L2-024 部落

L2-024 部落

作者:互联网

传送门

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e4 + 10;
int n,k,q,fa[maxn],kk[maxn];

int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void merge(int x,int y) {
	fa[find(x)] = find(y);
}

int main() {
	ios::sync_with_stdio(0);
	for(int i = 0; i < maxn; i++)
		fa[i] = i;
	cin >> n;
	int maxx = 0;
	while(n--) {
		cin >> k;
		for(int i = 0; i < k; i++) {
			cin >> kk[i];
			maxx = max(maxx,kk[i]);
		}
		for(int i = 0; i < k; i++) {
			for(int j = i + 1;j < k; j++)
				merge(kk[i],kk[j]);
		}
	}
	cin >> q;
	cout << maxx << " ";
	int cnt = 0;
	for(int i = 1; i <= maxx; i++) {
		if(fa[i]==i) cnt++;
	}
	cout << cnt << endl;
	while(q--) {
		int x,y;
		cin >> x >> y;
		if(find(x) == find(y))
			cout << "Y" << endl;
		else cout << "N" << endl;
	}
	return 0;
}

标签:fa,int,部落,++,cin,024,L2,kk,find
来源: https://blog.csdn.net/xcfkaixin/article/details/109955827