QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#256839 | #7755. Game on a Forest | ucup-team1951# | WA | 4ms | 12468kb | C++17 | 3.7kb | 2023-11-18 22:10:41 | 2023-11-18 22:10:43 |
Judging History
answer
// g++-13 3.cpp -std=c++17 -O2 -I .
#include <bits/stdc++.h>
using namespace std;
#include <algorithm>
#include <cassert>
#include <vector>
namespace atcoder {
struct dsu {
public:
dsu() : _n(0) {}
explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}
int merge(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y) return x;
if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}
bool same(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
int leader(int a) {
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0) return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}
int size(int a) {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
std::vector<std::vector<int>> groups() {
std::vector<int> leader_buf(_n), group_size(_n);
for (int i = 0; i < _n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<int>> result(_n);
for (int i = 0; i < _n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < _n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(
std::remove_if(result.begin(), result.end(),
[&](const std::vector<int>& v) { return v.empty(); }),
result.end());
return result;
}
private:
int _n;
std::vector<int> parent_or_size;
};
} // namespace atcoder
using namespace atcoder;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vst = vector<string>;
using vvst = vector<vst>;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define pq_big(T) priority_queue<T,vector<T>,less<T>>
#define pq_small(T) priority_queue<T,vector<T>,greater<T>>
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),a.end())
void dfs(int now,int par,vvi &g,vi &s0,vi &s1,vi &sz){
for(int nxt:g[now]){
if(nxt==par){
continue;
}
dfs(nxt,now,g,s0,s1,sz);
if(sz[nxt]%2==0){
s0[now]++;
}
else{
s1[now]++;
}
sz[now]+=sz[nxt];
}
sz[now]++;
s0[now]%=2;
s1[now]%=2;
sz[now]%=2;
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;cin>>n>>m;
dsu d(n);
vector<pair<int,int>> e(m);
vvi g(n);
rep(i,0,m){
int u,v;cin>>u>>v;
u--;v--;
e[i]={u,v};
d.merge(u,v);
g[u].emplace_back(v);
g[v].emplace_back(u);
}
vvi v=d.groups();
int grundy=0;
for(vi vv:v){
grundy^=(2-vv.size()%2);
}
vi s0(n,0),s1(n,0),sz(n,0);
rep(i,0,n){
if(sz[i]==0){
dfs(i,-1,g,s0,s1,sz);
}
}
int ans=0;
rep(i,0,m){
auto [u,v]=e[i];
int g2=grundy;
if(d.size(i)%2==0)g2^=2;
if(g2==0){
// cerr<<i<<" : "<<u<<" "<<v<<endl;
ans++;
}
}
rep(i,0,n){
int g2=grundy;
g2^=(2-d.size(i)%2);
int remain=d.size(i)-sz[i];
g2^=(2-remain%2);
if(s0[i]){
g2^=2;
}
if(s1[i]){
g2^=1;
}
if(g2==0){
// cerr<<i<<" : "<<i<<endl;
ans++;
}
}
cout<<ans<<endl;
}
/*
1-2 3 grundy 3
->
2 3 grundy 0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
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: -100
Wrong Answer
time: 4ms
memory: 12468kb
input:
100000 1 4647 17816
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'