社区讨论

50分WA on #23457求条

P1330封锁阳光大学参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@mk42u2uj
此快照首次捕获于
2026/01/07 21:51
2 个月前
此快照最后确认于
2026/01/10 19:05
2 个月前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;

int n, m;

vector<int> e[10001];
int color[10001];
bool vis[10001];

void bfs(int x) {
	queue<int> q;
	q.push(x);
	color[x] = 1;
	while (!q.empty()) {
		int t = q.front();
		q.pop();
		vis[t] = true;
		for (int s : e[t]) {
			int col = !color[t];
			if (!vis[s]) {
				color[s] = col;
				q.push(s);
			} else if (color[s] == color[t]) {
				cout << "Impossible";
				exit(0);
			}
		}
	}
}

signed main() {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int u, v;
		cin >> u >> v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	for (int i = 1; i <= n; i++) {//  处理非连通块
		if (!vis[i]) {
			bfs(i);
		}
	}
	int cnt1 = 0, cnt0 = 0;
	for (int i = 1; i <= n; i++) {
		if (color[i] == 1) {
			cnt1++;
		} else {
			cnt0++;
		}
	}
	cout << min(cnt1, cnt0);
	return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...