QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#256843#7755. Game on a Forestucup-team1951#WA 9ms12600kbC++173.7kb2023-11-18 22:12:132023-11-18 22:12:13

Judging History

你现在查看的是最新测评结果

  • [2023-11-18 22:12:13]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:12600kb
  • [2023-11-18 22:12:13]
  • 提交

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;
    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
*/

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

4 3
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 4ms
memory: 12552kb

input:

100000 1
4647 17816

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 4ms
memory: 12524kb

input:

100000 2
64075 72287
63658 66936

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 4ms
memory: 12476kb

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: 12600kb

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: 9ms
memory: 12484kb

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'