QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#575955#7755. Game on a ForestSCUTkkWA 0ms6672kbC++201.5kb2024-09-19 17:38:092024-09-19 17:38:09

Judging History

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

  • [2024-09-19 17:38:09]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:6672kb
  • [2024-09-19 17:38:09]
  • 提交

answer

#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m;
    std::cin >> n >> m;

    std::vector<std::vector<int>> adj(n);
    std::vector<std::pair<int, int>> edge(m);
    for (int i = 0; i < m; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;

        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
        edge[i] = {u, v};
    }

    std::vector<int> sz(n), sg(n), f(n, -1);
    auto dfs = [&](auto dfs, int x, int fa) -> void {
        f[x] = fa;
        sz[x] = 1;
        for (auto y : adj[x]) {
            if (y == fa)
                continue;
            dfs(dfs, y, x);
            sz[x] += sz[y];
        }
        sg[x] = (sz[x] & 1) ? 1 : 2;
    };

    int tot = 0;
    for (int i = 0; i < n; i++) {
        if (f[i] == -1) {
            dfs(dfs, i, -1);
            tot ^= sg[i];
        }
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
        int tmp = tot;
        for (auto j : adj[i]) {
            if (j == f[i])
                continue;
            tmp ^= sg[j];
        }
        tmp ^= sg[i];
        if (tmp)
            ans++;
    }

    for (auto [u, v] : edge) {
        if (f[v] != u)
            std::swap(u, v);

        int tmp = tot;
        tmp ^= sg[u] ^ sg[v] ^ (((sz[u] - sz[v]) & 1) ? 1 : 2);
        if (tmp)
            ans++;
    }

    std::cout << ans - 1 << "\n";

    return 0;
}

詳細信息

Test #1:

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

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

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

input:

100000 1
4647 17816

output:

99999

result:

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