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;
}


上一篇:Qt获取文件路径


下一篇:025 python继承与派生