其他分享
首页 > 其他分享> > L2-025 分而治之

L2-025 分而治之

作者:互联网

传送门

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

const int maxn = 10010;
int n,m,k,fa[maxn],a[maxn][maxn],v[maxn];

struct node {
	int x,y;
} e[maxn];
void init() {
	for(int i = 0; i < maxn; i++)
		fa[i] = i;
}

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);
	init();
	cin >> n >> m;
	for(int i = 0; i < m; i++) {
		cin >> e[i].x >> e[i].y;
		merge(e[i].x,e[i].y);
	}
	cin >> k;
	while(k--) {
		int cnt = 0;
		memset(v,0,sizeof(v));
		int p;
		cin >> p;
		for(int i = 0; i < p; i++) {
			int u;
			cin >> u;
			v[u]=1;
		}

		init();
		for(int i = 0; i < m; i++) {
			if(!v[e[i].x] && !v[e[i].y]) {
				merge(e[i].x,e[i].y);
			}
		}
		for(int i = 1; i <= n; i++) {
			if(fa[i] == i) cnt++;
		}
		if(cnt == n) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}


标签:int,分而治之,++,cin,025,fa,maxn,L2,find
来源: https://blog.csdn.net/xcfkaixin/article/details/110005924