QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#575955 | #7755. Game on a Forest | SCUTkk | WA | 0ms | 6672kb | C++20 | 1.5kb | 2024-09-19 17:38:09 | 2024-09-19 17:38:09 |
Judging History
answer
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>> adj(n);
std::vector<std::pair<int, int>> edge(m);
for (int i = 0; i < m; i++) {
int u, v;
std::cin >> u >> v;
u--, v--;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
edge[i] = {u, v};
}
std::vector<int> sz(n), sg(n), f(n, -1);
auto dfs = [&](auto dfs, int x, int fa) -> void {
f[x] = fa;
sz[x] = 1;
for (auto y : adj[x]) {
if (y == fa)
continue;
dfs(dfs, y, x);
sz[x] += sz[y];
}
sg[x] = (sz[x] & 1) ? 1 : 2;
};
int tot = 0;
for (int i = 0; i < n; i++) {
if (f[i] == -1) {
dfs(dfs, i, -1);
tot ^= sg[i];
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
int tmp = tot;
for (auto j : adj[i]) {
if (j == f[i])
continue;
tmp ^= sg[j];
}
tmp ^= sg[i];
if (tmp)
ans++;
}
for (auto [u, v] : edge) {
if (f[v] != u)
std::swap(u, v);
int tmp = tot;
tmp ^= sg[u] ^ sg[v] ^ (((sz[u] - sz[v]) & 1) ? 1 : 2);
if (tmp)
ans++;
}
std::cout << ans - 1 << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3756kb
input:
3 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
4 3 1 2 2 3 3 4
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 6672kb
input:
100000 1 4647 17816
output:
99999
result:
wrong answer 1st numbers differ - expected: '1', found: '99999'