QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#351030 | #6729. Unrooted Trie | Dream_56 | WA | 0ms | 3524kb | C++14 | 2.8kb | 2024-03-11 12:59:21 | 2024-03-11 12:59:22 |
Judging History
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]--;
}
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();
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3524kb
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:
1 0
result:
wrong answer 1st numbers differ - expected: '2', found: '1'