QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#351033#6729. Unrooted TrieDream_56RE 0ms3536kbC++142.8kb2024-03-11 13:05:302024-03-11 13:05:31

Judging History

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

  • [2024-03-11 13:05:31]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3536kb
  • [2024-03-11 13:05:30]
  • 提交

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';
                }
            }
        }
    }

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

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
Runtime Error

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:


result: