当前位置:网站首页>UVA 11080 – place the guards

UVA 11080 – place the guards

2022-07-07 21:04:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

UVA 11080 – Place the Guards

Topic link

The question : Some cities . There are roads between them , Now we're going to place guards , The guard can watch the edge around the current point , There can only be one guard on one side , Ask if there is a plan , Suppose there are at least a few guards

Ideas : Bipartite graph decision , Record the number of white dots and black dots in the determination process , The small one is the number to be placed , Note that the assumption is 0, Then it should be plus 1

Code :

#include <cstdio>#include <cstring>#include <vector>using namespace std;const int N = 205;int color[N];vector<int> g[N];int b, w;int bipartite(int u) {	if (color[u] == 1) b++;	if (color[u] == 2) w++;	for (int i = 0; i < g[u].size(); i++) {		int v = g[u][i];		if (color[u] == color[v]) return false;		if (!color[v]) {			color[v] = 3 - color[u];			if (!bipartite(v)) return false;		}	}	return true;}int t, n, m;int solve() {	int ans = 0;	for (int i = 0; i < n; i++) {		if (!color[i]) {			color[i] = 1;			b = w = 0;			if (!bipartite(i)) return -1;			ans += max(1, min(b, w));		}	}	return ans;}int main() {	scanf("%d", &t);	while (t--) {		scanf("%d%d", &n, &m);		for (int i = 0; i < n; i++) {			g[i].clear();			color[i] = 0;		}		int u, v;		while (m--) {			scanf("%d%d", &u, &v);			g[u].push_back(v);			g[v].push_back(u);		}		printf("%d\n", solve());	}	return 0;}

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116454.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071843026542.html