QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#619164 | #7755. Game on a Forest | lixp | WA | 2ms | 6136kb | C++14 | 1.2kb | 2024-10-07 13:22:44 | 2024-10-07 13:22:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int fa[N],n,m,vis[N],siz[N],sz[N];
int fir[N],ne[N<<1],to[N<<1],cnt;
int sum,ans;
void add(int x,int y) {
ne[++cnt]=fir[x];
fir[x]=cnt;
to[cnt]=y;
}
int gts(int x) {
if(fa[x]!=x) fa[x]=gts(fa[x]);
return fa[x];
}
void dfs(int x,int ff) {
sz[x]=1;vis[x]=1;
int nans=ans; nans^=siz[gts(x)]&1?1:2;
for(int i=fir[x];i;i=ne[i]) {
int y=to[i];
if(y==ff) continue;
dfs(y,x); sz[x]+=sz[y];
int nw=ans^(sz[y]&1?1:2)^((siz[gts(x)]-sz[y])&1?1:2)^(siz[gts(x)]&1?1:2);
if(nw==0) sum++;
nans^=sz[y]&1?1:2;
}
// cout<<"! "<<x<<" "<<sum<<" "<<"? "<<siz[gts(x)]<<" "<<sz[x]<<" "<<nans<<" ";
nans^=(siz[gts(x)]-sz[x])&1?1:2;
if(nans==0) sum++;
// cout<<sum<<"\n";
}
int main() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) fa[i]=i,siz[i]=1;
for(int i=1;i<=m;i++) {
int x,y;scanf("%d%d",&x,&y);
add(x,y);add(y,x);
siz[gts(x)]+=siz[gts(y)];
fa[gts(y)]=gts(x);
}
for(int i=1;i<=n;i++) {
if(fa[i]!=i) continue;
ans^=siz[i]&1?1:2;
// cout<<siz[i]<<"\n";
}
if(ans==0) {
cout<<"0\n";
return 0;
}
for(int i=1;i<=n;i++) {
if(!vis[i]) dfs(i,0);
}
cout<<sum;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3776kb
input:
3 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5872kb
input:
4 3 1 2 2 3 3 4
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5932kb
input:
100000 1 4647 17816
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 1ms
memory: 5980kb
input:
100000 2 64075 72287 63658 66936
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 2ms
memory: 5164kb
input:
100000 3 59930 72281 31689 59132 20469 33165
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 1ms
memory: 6136kb
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: 1ms
memory: 6036kb
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:
99898
result:
wrong answer 1st numbers differ - expected: '200', found: '99898'