团体程序设计天梯赛 L2-025 分而治之 (25分)

题目链接:

L2-025 分而治之 (25分)

思路:

对每种方案,检查每个未被攻占的城市的相邻城市是否全被攻占即可

代码:

#include<bits/stdc++.h>

using namespace std;

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

const int maxn = 1e4 + 5;
vector<int> G[maxn];
bool lost[maxn];
inline bool ok(int & n) {
	for(int i = 1; i <= n; i++) if(!lost[i]) {
		for(int & x : G[i]) if(!lost[x]) return false;
	}
	return true;
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif	
	int n = read(), m = read();
	for(int i = 0; i < m; i++) {
		int x = read(), y = read();
		G[x].push_back(y);
		G[y].push_back(x);	
	}
	for(int k = read(); k--;) {
		fill(lost, lost + n + 1, 0);	
		for(int np = read(); np--;) {
			int x = read();
			lost[x] = true;	
		}
		puts(ok(n) ? "YES" : "NO");
	}
	return 0;
}

发布了299 篇原创文章 · 获赞 8 · 访问量 7445

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/104065371