QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#616244#6729. Unrooted TrieAndycipationWA 270ms29968kbC++202.2kb2024-10-05 23:52:032024-10-05 23:52:05

Judging History

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

  • [2024-10-05 23:52:05]
  • 评测
  • 测评结果:WA
  • 用时:270ms
  • 内存:29968kb
  • [2024-10-05 23:52:03]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    [&] {
      int n;
      cin >> n;
      vector<vector<pair<int, char>>> g(n);
      for (int i = 0; i < n - 1; i++) {
        int x, y;
        char c;
        cin >> x >> y >> c;
        --x; --y;
        g[x].emplace_back(y, c);
        g[y].emplace_back(x, c);
      }
      for (int i = 0; i < n; i++) {
        map<char, int> cnt;
        int k2 = 0;
        for (auto [_, c] : g[i]) {
          cnt[c] += 1;
          if (cnt[c] == 3) {
            cout << 0 << '\n';
            return;
          }
          if (cnt[c] == 2) {
            k2 += 1;
            if (k2 == 2) {
              cout << 0 << '\n';
              return;
            }
          }
        }
      }
      vector<int> pre(n);
      vector<int> post(n);
      vector<int> order;
      vector<int> bad(n + 1);
      function<void(int, int)> Dfs = [&](int v, int p) {
        pre[v] = order.size();
        order.push_back(v);
        char pe = '.';
        for (auto [u, c] : g[v]) {
          if (u == p) {
            pe = c;
            continue;
          }
          Dfs(u, v);
        }
        post[v] = order.size() - 1;
        set<char> seen;
        for (auto [u, c] : g[v]) {
          if (u == p) {
            continue;
          }
          if (seen.count(c)) {
            bad[0] += 1;
            bad[pre[v] + 1] -= 1;
            bad[post[v] + 1] += 1;
            bad[n] -= 1;
            break;
          }
          seen.insert(c);
          if (c == pe) {
            bad[pre[v]] += 1;
            bad[pre[v] + 1] -= 1;
            for (auto [uu, cc] : g[v]) {
              if (cc != c) {
                bad[pre[uu]] += 1;
                bad[post[uu] + 1] -= 1;
              }
            }
            break;
          }
        }
      };
      Dfs(0, -1);
      int ret = 0;
      for (int i = 0; i < n; i++) {
        if (bad[i] == 0) {
          ret += 1;
        }
        bad[i + 1] += bad[i];
      }
      cout << ret << '\n';
    }();
  }
  return 0;
}

详细

Test #1:

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

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: -100
Wrong Answer
time: 270ms
memory: 29968kb

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
15
34
70
37
39
19
24
58
15
16
14
10
73
75
65
35
45
33
81
46
35
78
49
28
13
32
10
33
48
47
3
9
50
8
37
15
84
23
75
26
36
35
61
65
58
30
56
11
8
39
60
88
40
56
17
44
62
12
19
2
59
22
54
15
91
87
1
80
11
45
69
81
33
87
46
56
62
54
80
7
6
48
20
55
19
9
4
38
39
89
35
63
46
32
7
...

result:

wrong answer 10th numbers differ - expected: '4', found: '15'