题解
2025-07-21 18:43:44
发布于:广东
0阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> deg(n + 1, 0);
vector<vector<int>> graph(n + 1);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
deg[a]++;
deg[b]++;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<bool> visited(n + 1, false);
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
queue<int> q;
q.push(i);
visited[i] = true;
vector<int> comp;
comp.push_back(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : graph[u]) {
if (!visited[v]) {
visited[v] = true;
q.push(v);
comp.push_back(v);
}
}
}
int total_deg = 0;
int odd_count = 0;
for (int node : comp) {
total_deg += deg[node];
if (deg[node] % 2 == 1) {
odd_count++;
}
}
if (total_deg == 0) {
continue;
}
if (odd_count == 0) {
ans++;
} else {
ans += odd_count / 2;
}
}
}
cout << ans << endl;
return 0;
}
这里空空如也
有帮助,赞一个