QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#658827#7755. Game on a ForestsleighWA 2ms6408kbC++202.2kb2024-10-19 17:42:172024-10-19 17:42:17

Judging History

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

  • [2024-10-19 17:42:17]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6408kb
  • [2024-10-19 17:42:17]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int N = 1e5 + 10;

//////////////////////////////////////

struct {
    int to, next;
} edge[N << 1];;

int head[N];
int cnt;

void add(const int u, const int v) {
    edge[++cnt] = {v, head[u]};
    head[u]     = cnt;
}

//////////////////////////////////////

struct node {
    int fa, tree, sz = 1;
};

node nodes[N];

void dfs(const int tree, const int fa, const int now) {
    nodes[now].fa   = fa;
    nodes[now].tree = tree;
    for (int i = head[now]; i; i = edge[i].next) {
        const int to = edge[i].to;
        if (to == fa) continue;
        dfs(tree, now, to);
        nodes[now].sz += nodes[to].sz;
    }
}

//////////////////////////////////////

int sg  = 0;
int ans = 0;

int get_sg(int sz) {
    if (sz == 0) return 0;
    else if (sz & 1) return 1;
    else return 2;
}

void dian(int fa, int now) {
    int nsg = sg;
    for (int i = head[now]; i; i = edge[i].next) {
        const int to = edge[i].to;
        if (to == fa) continue;
        nsg ^= get_sg(nodes[to].sz);
    }
    if ((nsg ^ get_sg(nodes[fa].sz - nodes[now].sz)) == 0) {
        ans++;
    }
}

void bian(int fa, int now) {
    if ((sg ^ get_sg(nodes[fa].sz - nodes[now].sz) ^ get_sg(nodes[now].sz)) == 0) {
        ans++;
    }
}

void dfs2(int fa, int now) {
    dian(fa, now);
    if (fa != now) bian(fa, now);
    for (int i = head[now]; i; i = edge[i].next) {
        const int to = edge[i].to;
        if (to == fa) continue;
        dfs2(now, to);
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        add(u, v);
        add(v, u);
    }
    for (int i = 1; i <= n; i++) {
        if (nodes[i].tree == 0) {
            dfs(i, i, i);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (nodes[i].tree == i) {
            sg ^= get_sg(nodes[i].sz);
        }
    }

    for (int i = 1; i <= n; i++) {
        if (nodes[i].tree == i) {
            sg ^= get_sg(nodes[i].sz);
            dfs2(i, i);
            sg ^= get_sg(nodes[i].sz);
        }
    }

    cout << ans;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 4828kb

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 1ms
memory: 4748kb

input:

4 3
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 2ms
memory: 6188kb

input:

100000 1
4647 17816

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 2ms
memory: 6408kb

input:

100000 2
64075 72287
63658 66936

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 1ms
memory: 5928kb

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

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: 2ms
memory: 5068kb

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:

199

result:

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