QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#243280#6729. Unrooted TrieCipherxzc#WA 194ms27544kbC++201.9kb2023-11-08 00:14:242023-11-08 00:14:24

Judging History

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

  • [2023-11-08 00:14:24]
  • 评测
  • 测评结果:WA
  • 用时:194ms
  • 内存:27544kb
  • [2023-11-08 00:14:24]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
#define LL long long
#define mp make_pair

const int N = 1e5 + 5;
int T, n, head[N], to[N * 2], nxt[N * 2], len[N * 2], tot, cnt, f[N][30], ans;
bool ban[N];

inline void add(int u, int v, int w){
    to[++tot] = v;
    nxt[tot] = head[u];
    head[u] = tot;
    len[tot] = w;
}

inline void calc(int p){
    bool flag = true;
    for (int i = 0; i < 26; i++){
        if (f[p][i] > 1){
            flag = false;
            break;
        }
    }
    if (!flag && !ban[p]){
        ban[p] = true;
        cnt++;
    }else if (flag && ban[p]){
        ban[p] = false;
        cnt--;
    }
}

void dfs(int p, int fa){
    for (int i = head[p]; i; i = nxt[i]){
        int q = to[i];
        if (q == fa){
            continue;
        }
        f[p][len[i]]++;
        dfs(q, p);
    }
    calc(p);
}

void dfs2(int p, int fa){
    if (!cnt){
        ans++;
    }
    int gg = cnt;
    for (int i = head[p]; i; i = nxt[i]){
        int q = to[i];
        if (q == fa){
            continue;
        }
        f[p][len[i]]--;
        f[q][len[i]]++;
        calc(p); calc(q);
        dfs2(q, p);
        f[q][len[i]]--;
        f[p][len[i]]++;
        cnt = gg;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> T;
    while (T--){
        cin >> n;
        
        for (int i = 1; i <= n; i++){
            ban[i] = head[i] = 0;
            memset(f[i], 0, sizeof(f[i]));
        }
        ans = cnt = tot = 0;

        char ch;
        for (int i = 1, u, v; i < n; i++){
            cin >> u >> v >> ch;
            add(u, v, ch - 'a'); add(v, u, ch - 'a');
        }

        dfs(1, 0);
        dfs2(1, 0);

        cout << ans << endl;
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 194ms
memory: 27544kb

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

result:

wrong answer 8th numbers differ - expected: '27', found: '25'