QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#351036#6729. Unrooted TrieDream_56WA 89ms5468kbC++142.9kb2024-03-11 13:09:202024-03-11 13:09:20

Judging History

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

  • [2024-03-11 13:09:20]
  • 评测
  • 测评结果:WA
  • 用时:89ms
  • 内存:5468kb
  • [2024-03-11 13:09:20]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using li = long long;
using pli = pair<long long, int>;

struct Graph {
public:
    int n, m;
    vector<int> head, to, nxt;
    vector<char> val;
    Graph(int n, int m): head(n + 5), to((m << 1) + 5), val((m << 1) + 5), nxt((m << 1) +5) {}
    void adde(int x, int y, char w) {
        to[tot] = y, val[tot] = w, nxt[tot] = head[x], head[x] = tot, tot++;
        swap(x, y);
        to[tot] = y, val[tot] = w, nxt[tot] = head[x], head[x] = tot, tot++;
    }
private:
    int tot = 2;
};

void solve() {
    int n; cin >> n;
    Graph Tree(n, n - 1);
    for (int i = 1; i < n; ++i) {
        int x, y; 
        char w;
        cin >> x >> y >> w;
        Tree.adde(x, y, w);
    }

    int point = -1, x, y;
    for (int i = 1; i <= n; ++i) {
        vector<vector<int>> ch(26);
        for (int ei = Tree.head[i]; ei; ei = Tree.nxt[ei]) {
            int v = Tree.to[ei], val = Tree.val[ei] - 'a';
            ch[val].push_back(v);
            if (ch[val].size() > 2) {
                cout << 0 << '\n';
                return;
            } else if (ch[val].size() == 2) {
                if (point != -1) {
                    cout << 0 << '\n';
                    return;
                } else {
                    // cout << ch[val][0] << " " << ch[val][1] << '\n';
                    point = i;
                    x = ch[val][0], y = ch[val][1];
                    // cout << point << " ! " << x << " " << y << '\n';
                }
            }
        }
    }

    if (point == -1) {
        cout << n << '\n';
        return;
    }

    int dfnid = 0;
    vector<int> bgn(n + 5), fin(n + 5);
    auto dfs = [&](auto self, int u, int fa) -> void {
        bgn[u] = ++dfnid;
        for (int ei = Tree.head[u]; ei; ei = Tree.nxt[ei]) {
            int v = Tree.to[ei];
            if (v == fa) 
                continue;
            self(self, v, u);
        }
        fin[u] = dfnid;
    };
    dfs(dfs, 1, 0);

    // cout << point << " " << x << " " << y << '\n';

    if (bgn[x] > bgn[y]) 
        swap(x, y);
    // cout << point << " " << x << " " << y << '\n';
    // cout << bgn[x] <//< " " << fin[x] << " " << bgn[y] << " " << fin[y] << '\n';
    vector<int> f(n + 5);
    if (bgn[point] < bgn[x] and bgn[point] < bgn[y]) {
        f[1]++, f[n + 1]--;
        f[bgn[x]]--, f[fin[x] + 1]++;
        f[bgn[y]]--, f[fin[y] + 1]++;
    } else {
        f[bgn[point]]++, f[fin[point] + 1]--;
        f[bgn[y]]--, f[fin[y]+1]++;
    }

    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        // cout << f[i] << " ";
        f[i] += f[i - 1];
        // cout << f[i] << '\n';
        if (f[i] == 0) {
            ++ans;
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T; cin >> T;
    while (T--) {
        solve();
    }
    
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 89ms
memory: 5468kb

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:

0
0
0
0
34
17
0
0
3
0
0
70
0
0
19
0
58
0
0
0
0
0
0
0
0
45
0
0
0
0
0
0
0
0
0
10
0
48
0
0
0
0
0
0
0
0
23
75
26
0
0
0
0
58
0
0
0
8
0
0
0
0
0
17
0
0
0
0
2
0
0
54
0
0
0
1
0
11
0
0
0
0
87
0
0
0
0
0
0
0
0
0
0
19
9
4
0
39
0
35
0
46
0
7
0
4
0
0
6
8
5
56
0
0
0
0
0
49
0
0
0
0
0
9
0
0
0
5
0
0
0
0
0
0
72
0
0
56
...

result:

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