QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620502 | #7755. Game on a Forest | 1025497292 | WA | 3ms | 8776kb | C++17 | 2.4kb | 2024-10-07 18:38:04 | 2024-10-07 18:38:04 |
Judging History
answer
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define Mirai ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
typedef pair<int,int> pii;
const int N=1e5+10;
void solve()
{
int n,m;cin>>n>>m;
vector<vector<int>> g(n+1);
vector<int> p(n+1);
iota(p.begin(),p.end(),0);
auto find=[&](auto &&self,int x)->int{
if(p[x]!=x)return p[x]=self(self,p[x]);
return p[x];
};
for(int i=1;i<=m;i++)
{
int u,v;cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
int pu=find(find,u),pv=find(find,v);
if(pu!=pv)
{
p[pu]=pv;
}
}
vector<int> sz(n+1);
vector<int> Xor(n+1);//子树SG值异或和
vector<int> sg(n+1);
auto dfs=[&](auto &&self,int u,int father)->void{
sz[u]=1;
for(auto v:g[u])
{
if(v==father)continue;
self(self,v,u);
sz[u]+=sz[v];
if(sz[v]&1)Xor[u]^=1;
else Xor[u]^=2;
}
};
int sum=0;
for(int i=1;i<=n;i++)
{
if(p[i]==i)
{
dfs(dfs,i,-1);
// cout<<i<<" "<<sz[i]<<endl;
if(sz[i]&1)sum^=1;
else sum^=2;
}
}
for(int i=1;i<=n;i++)
{
if(sz[i]&1)sg[i]=1;
else sg[i]=2;
}
// cout<<Xor<<endl;
int res=0;
auto dfs2=[&](auto &&self,int u,int father,int other)->void{
int fasz=sz[find(find,u)]-sz[u];
if(fasz&1)fasz=1;
else fasz=2;
if((fasz^Xor[u]^other)==0)
{
res++;
// cout<<u<<" ";
// cout<<fasz<<" "<<Xor[u]<<" "<<other<<endl;
}
fasz=3-fasz;//带上自己
for(auto v:g[u])
{
if(v==father)continue;
self(self,v,u,other);
if((other^fasz^sg[v])==0)
{
res++;
// cout<<u<<" "<<v<<" ";
// cout<<other<<" "<<fasz<<" "<<sg[v]<<endl;
}
}
};
for(int i=1;i<=n;i++)
{
if(p[i]==i)
{
dfs2(dfs2,i,-1,sum^sg[i]);
}
}
cout<<res<<endl;
}
signed main()
{
Mirai;
int T=1;
// cin>>T;
while(T--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3832kb
input:
3 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3616kb
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: 8776kb
input:
100000 1 4647 17816
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 8584kb
input:
100000 2 64075 72287 63658 66936
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 3ms
memory: 8572kb
input:
100000 3 59930 72281 31689 59132 20469 33165
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 0ms
memory: 8600kb
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: 0ms
memory: 8740kb
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'