QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#502016 | #7755. Game on a Forest | nikatamliani# | WA | 0ms | 7788kb | C++14 | 1.8kb | 2024-08-03 00:31:54 | 2024-08-03 00:31:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#else
#define debug(...)
#endif
#define int long long
main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M; cin >> N >> M;
vector<vector<int>> g(N);
vector<pair<int, int>> edges(M);
for (int i = 0; i < M; ++i) {
int u, v; cin >> u >> v; --u, --v;
edges[i] = {u, v};
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> parent(N), sub(N), root(N);
auto dfs = [&](auto self, int x, int p = -1) -> void {
sub[x] = 1;
parent[x] = p;
root[x] = p == -1 ? x : root[p];
for (int to : g[x]) {
if (to != p) {
self(self, to, x);
sub[x] += sub[to];
}
}
};
dfs(dfs, 0, -1);
vector<int> parity(2);
for (int i = 0; i < N; ++i) {
if (parent[i] == -1) {
parity[sub[i] & 1] ^= 1;
}
}
int total = 0;
for (int i = 0; i < N; ++i) { // let's remove node i
int tree_size = sub[root[i]];
auto save = parity;
parity[tree_size & 1] ^= 1;
for (int to : g[i]) {
int s = to == parent[i] ? tree_size - sub[i] : sub[to];
parity[s & 1] ^= 1;
}
if (parity[0] == 0 && parity[1] == 0) {
++total;
}
parity = save;
}
for (pair<int, int> e : edges) { // let's remove this edge
if (parent[e.first] == e.second) {
swap(e.first, e.second);
}
int tree_size = sub[root[e.first]];
auto save = parity;
parity[tree_size & 1] ^= 1;
int sub_first = tree_size - sub[e.second];
int sub_second = sub[e.second];
parity[sub_first & 1] ^= 1;
parity[sub_second & 1] ^= 1;
if (parity[0] == 0 && parity[1] == 0) {
++total;
}
parity = save;
}
cout << total << endl;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3532kb
input:
3 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3600kb
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: 7788kb
input:
100000 1 4647 17816
output:
99998
result:
wrong answer 1st numbers differ - expected: '1', found: '99998'