QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#502016#7755. Game on a Forestnikatamliani#WA 0ms7788kbC++141.8kb2024-08-03 00:31:542024-08-03 00:31:55

Judging History

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

  • [2024-08-03 00:31:55]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:7788kb
  • [2024-08-03 00:31:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "debug.cpp"
#else
#define debug(...)
#endif

#define int long long

main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int N, M; cin >> N >> M;
  vector<vector<int>> g(N);
  vector<pair<int, int>> edges(M);
  for (int i = 0; i < M; ++i) {
    int u, v; cin >> u >> v; --u, --v;
    edges[i] = {u, v};
    g[u].push_back(v);
    g[v].push_back(u);
  }

  vector<int> parent(N), sub(N), root(N);
  auto dfs = [&](auto self, int x, int p = -1) -> void {
    sub[x] = 1;
    parent[x] = p;
    root[x] = p == -1 ? x : root[p];
    for (int to : g[x]) {
      if (to != p) {
        self(self, to, x);
        sub[x] += sub[to];
      }
    }
  };

  dfs(dfs, 0, -1);

  vector<int> parity(2);
  for (int i = 0; i < N; ++i) {
    if (parent[i] == -1) {
      parity[sub[i] & 1] ^= 1;
    }
  }

  int total = 0;

  for (int i = 0; i < N; ++i) { // let's remove node i
    int tree_size = sub[root[i]];
    
    auto save = parity;
    parity[tree_size & 1] ^= 1;
    for (int to : g[i]) {
      int s = to == parent[i] ? tree_size - sub[i] : sub[to];
      parity[s & 1] ^= 1;
    }

    if (parity[0] == 0 && parity[1] == 0) {
      ++total;
    }

    parity = save;
  }

  for (pair<int, int> e : edges) { // let's remove this edge
    if (parent[e.first] == e.second) {
      swap(e.first, e.second);
    }

    int tree_size = sub[root[e.first]];
    auto save = parity;
    parity[tree_size & 1] ^= 1;

    int sub_first = tree_size - sub[e.second];
    int sub_second = sub[e.second];
    parity[sub_first & 1] ^= 1;
    parity[sub_second & 1] ^= 1;

    if (parity[0] == 0 && parity[1] == 0) {
      ++total;
    }

    parity = save;
  }

  cout << total << endl;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

4 3
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 7788kb

input:

100000 1
4647 17816

output:

99998

result:

wrong answer 1st numbers differ - expected: '1', found: '99998'