QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#182759#6729. Unrooted Trieucup-team004AC ✓361ms88032kbC++201.7kb2023-09-18 15:05:412023-09-18 15:05:42

Judging History

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

  • [2023-09-18 15:05:42]
  • 评测
  • 测评结果:AC
  • 用时:361ms
  • 内存:88032kb
  • [2023-09-18 15:05:41]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

void solve() {
    int n;
    std::cin >> n;
    
    std::vector<std::vector<std::pair<int, char>>> adj(n);
    for (int i = 1; i < n; i++) {
        int u, v;
        char c;
        std::cin >> u >> v >> c;
        u--, v--;
        adj[u].emplace_back(v, c);
        adj[v].emplace_back(u, c);
    }
    
    int need = 0;
    std::vector<int> parent(n, -1), f(n);
    
    auto dfs = [&](auto self, int x) -> void {
        std::vector<int> ch[26];
        for (auto [y, c] : adj[x]) {
            ch[c - 'a'].push_back(y);
            if (y != parent[x]) {
                parent[y] = x;
                self(self, y);
            }
        }
        for (int c = 0; c < 26; c++) {
            if (ch[c].size() > 2) {
                need++;
            } else if (ch[c].size() == 2) {
                for (auto y : ch[c]) {
                    if (y == parent[x]) {
                        f[0]++;
                        f[x]--;
                    } else {
                        f[y]++;
                    }
                }
                need++;
            }
        }
    };
    dfs(dfs, 0);
    
    
    auto dfs1 = [&](auto self, int x) -> void {
        for (auto [y, c] : adj[x]) {
            if (y != parent[x]) {
                f[y] += f[x];
                self(self, y);
            }
        }
    };
    dfs1(dfs1, 0);
    
    int ans = std::count(f.begin(), f.end(), need);
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
6
3 1 a
3 2 a
3 4 b
4 5 c
4 6 d
6
3 1 a
3 2 a
3 4 b
5 4 c
6 4 c

output:

2
0

result:

ok 2 number(s): "2 0"

Test #2:

score: 0
Accepted
time: 361ms
memory: 88032kb

input:

1112
19
15 18 a
7 18 c
11 14 b
10 17 b
8 14 a
1 3 b
12 2 a
16 3 c
16 4 b
2 3 a
15 5 a
3 18 d
16 9 a
18 13 b
8 4 b
17 7 a
9 6 a
13 19 a
52
8 32 a
14 51 a
30 52 a
48 36 b
27 39 c
39 51 b
35 15 a
51 52 d
45 51 e
39 26 a
20 12 b
34 18 a
9 12 e
25 5 a
9 13 b
41 51 c
1 52 n
33 14 c
22 30 b
17 4 b
12 52 c
...

output:

15
49
22
68
34
17
28
27
3
4
34
70
37
39
19
24
58
8
16
14
10
73
73
65
35
45
33
81
46
35
78
49
22
13
26
10
33
48
47
3
9
50
8
37
15
84
23
75
26
35
35
61
65
58
30
56
11
8
39
60
88
40
56
17
42
62
12
11
2
59
22
54
14
91
87
1
80
11
45
69
80
33
87
46
56
62
54
80
7
4
48
20
55
19
9
4
38
39
89
35
63
46
24
7
52...

result:

ok 1112 numbers