QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#260966 | #7755. Game on a Forest | smartyi# | WA | 4ms | 9428kb | C++20 | 2.2kb | 2023-11-22 17:07:42 | 2023-11-22 17:07:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define Joyfine \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define int long long
const int N = 1e5 + 10;
int _ = 1;
int n, m;
vector<int> g[N];
int fa[N], siz[N], sz[N];
int sg[3];
int f[N][2];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int u, int v) {
int fu = find(u);
int fv = find(v);
if (fu != fv) {
fa[fv] = fu;
siz[fu] += siz[fv];
}
}
void DFS(int u, int fa) {
for (auto v : g[u])
if (v != fa) {
DFS(v, u);
sz[u] = sz[u] + sz[v];
}
}
void dfs(int u, int fa) {
if (fa != 0) {
if ((siz[find(u)] - sz[u]) % 2)
f[u][1]++;
else
f[u][0]++;
}
for (auto v : g[u])
if (v != fa) {
if (sz[v] % 2)
f[u][1]++;
else
f[u][0]++;
}
for (auto v : g[u])
if (v != fa) dfs(v, u);
}
void solve(int ce) {
sg[1] = 1;
sg[0] = 2;
int ans = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1, sz[i] = 1;
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
merge(u, v);
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> v;
int sum = 0;
for (int i = 1; i <= n; i++) {
if (fa[i] == i) {
v.push_back(siz[i]);
sum ^= sg[siz[i] % 2];
}
}
for (auto i : v) {
if (sum == 2 && i % 2 == 0) {
ans += (i - 1);
} else if (sum == 3 && i % 2 == 1) {
ans += (i - 1);
}
}
for (int i = 1; i <= n; i++) {
if (fa[i] == i) {
DFS(i, 0);
dfs(i, 0);
}
}
for (int i = 1; i <= n; i++) {
// cout << siz[i] << endl;
int a1 = f[i][0] % 2, a2 = f[i][1] % 2;
if ((sum ^ sg[siz[find(i)] % 2] ^ (2 * a1) ^ (a2)) == 0) {
ans++;
}
// cout << f[i][0] << " " << f[i][1] << endl;
}
cout << ans;
}
signed main(void) {
Joyfine;
// cin>>_;
for (int i = 1; i <= _; i++) solve(i);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5740kb
input:
3 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5740kb
input:
4 3 1 2 2 3 3 4
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 0
Accepted
time: 0ms
memory: 8772kb
input:
100000 1 4647 17816
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 4ms
memory: 8800kb
input:
100000 2 64075 72287 63658 66936
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 0ms
memory: 8856kb
input:
100000 3 59930 72281 31689 59132 20469 33165
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 4ms
memory: 8844kb
input:
100000 10 20391 78923 27822 80617 21749 25732 12929 79693 42889 52515 59064 99869 29031 41875 4463 17813 13407 42498 19120 20957
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: -100
Wrong Answer
time: 4ms
memory: 9428kb
input:
99999 101 34378 94161 67696 83255 24557 25591 11476 58475 5684 38157 33843 35321 9046 24028 14293 77681 587 42098 9402 27228 6999 13980 27118 84005 3622 8353 13545 51621 16998 63647 32912 53178 15206 15815 56517 86335 5563 93770 153 278 11242 41753 75098 76792 1695 22836 25936 33352 2765 6778 19597 ...
output:
202
result:
wrong answer 1st numbers differ - expected: '200', found: '202'