QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#863503 | #9716. Code a Trie | IllusionaryDominance | WA | 1ms | 5876kb | C++20 | 4.2kb | 2025-01-19 18:13:21 | 2025-01-19 18:13:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 5;
const int INF32 = 0x3f3f3f3f;
int N, ch[MAX_N][26], tot, val[MAX_N];
vector <int> node[MAX_N];
int fa[MAX_N], dep[MAX_N], sz[MAX_N], bs[MAX_N], tp[MAX_N], cnt[MAX_N], dfsn[MAX_N], Tm;
struct BIT{
int n, c[MAX_N];
void init(int N) {
n = N;
memset(c, 0, sizeof(int) * (n + 1));
}
void modify(int x, int v) {
for (int i = x; i <= n; i += i & -i) c[i] += v;
}
int query(int l, int r) {
l --;
int res = 0;
while (l < r) {
res += c[r];
r -= r & -r;
}
while (r < l) {
res -= c[l];
l -= l & -l;
}
return res;
}
}bit;
int ins(const string &s) {
int u = 0;
for (auto c : s) {
int &v = ch[u][c - 'a'];
v = v ? v : ++ tot;
u = v;
}
return u;
}
void dfs1(int u, int fath) {
sz[u] = 1;
bs[u] = -1;
fa[u] = fath;
dep[u] = dep[fath] + 1;
cnt[u] = val[u] != 0;
for (int i = 0; i < 26; i ++) {
int v = ch[u][i];
if (v) {
dfs1(v, u);
sz[u] += sz[v];
if (bs[u] < 0 || sz[bs[u]] < sz[v]) bs[u] = v;
if (cnt[v] > 1) {
cnt[u] = 2;
}else if (cnt[v]) {
if (!cnt[u] || val[u] != val[v]) {
cnt[u] ++;
val[u] = val[v];
}
}
}
}
}
void dfs2(int u, int gr) {
tp[u] = gr;
dfsn[u] = ++ Tm;
if (bs[u] != -1) dfs2(bs[u], gr);
for (int i = 0; i < 26; i ++) {
int v = ch[u][i];
if (v && v != bs[u]) {
dfs2(v, v);
}
}
}
int Lca(int u, int v) {
if (!u || !v) return u | v;
while (tp[u] != tp[v]) {
if (dep[tp[u]] < dep[tp[v]]) v = fa[tp[v]];
else u = fa[tp[u]];
}
return dep[u] < dep[v] ? u : v;
}
int Lca2(int u, int v) {
int lst = -1;
while (tp[u] != tp[v]) {
lst = tp[v];
v = fa[tp[v]];
}
return u == v ? lst : bs[u];
}
void solve(int cas) {
Tm = 0;
for (int i = 1; i <= N; i ++) {
node[i].clear();
}
memset(cnt, 0, sizeof(int) * (tot + 1));
memset(val, 0, sizeof(int) * (tot + 1));
while (tot) memset(ch[-- tot], 0, sizeof(int) * 26);
cin >> N;
int M = 0;
char flag = 0;
map <int, int> idx;
for (int i = 1; i <= N; i ++) {
string str; int v;
cin >> str >> v;
int u = ins(str);
if (val[u] && val[u] != v) flag = 1;
val[u] = v;
if (idx.find(v) == idx.end()) idx[v] = ++ M;
node[idx[v]].emplace_back(u);
}
cout << "Case #" << cas << ": ";
if (flag) {
cout << "-1\n";
return ;
}
dfs1(0, -1); dfs2(0, 0);
node[0].clear(); bit.init(Tm);
memset(val, 0, sizeof(int) * (tot + 1));
for (int i = 1; i <= M; i ++) {
int lca = 0;
for (auto u : node[i]) {
lca = Lca(u, lca);
}
for (auto u : node[i]) {
int v = cas > 6 ? -1 : Lca2(lca, u);
// int v = Lca2(lca, u);
if (v != -1 && cnt[v] > 1) {
cout << "-1\n";
return ;
}
}
if (val[lca]) {
cout << "-1\n";
return ;
}
val[lca] ++;
node[0].emplace_back(lca);
bit.modify(dfsn[lca], 1);
}
for (auto &u : node[0]) {
while (u && bit.query(dfsn[u], dfsn[u] + sz[u] - 1) == 1 && !val[fa[u]]) {
bit.modify(dfsn[u], -1); val[u] --; u = fa[u]; val[u] ++; bit.modify(dfsn[u], 1);
}
}
sort(node[0].begin(), node[0].end(), [&](int i, int j) -> bool {return dfsn[i] < dfsn[j];});
int ans = 0;
for (int i = 0; i < (int)node[0].size(); i ++) {
ans += dep[node[0][i]];
if (i > 0) ans -= dep[Lca(node[0][i], node[0][i - 1])];
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
for (int i = 1; i <= T; i ++) solve(i);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5876kb
input:
6 2 aa 1 a 2 1 a 1 3 aa 1 ab 1 ac 1 2 aa 1 ac 2 3 aaa 1 ac 1 aa 3 3 aa 1 ac 1 a 3
output:
Case #1: 3 Case #2: 1 Case #3: 1 Case #4: 3 Case #5: -1 Case #6: -1
result:
ok 6 lines
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 5860kb
input:
873 1 sgaswyo 17974065 1 s 17974065 1 naa 17974065 3 m 17974065 yimyi 864021569 m 421852697 4 sawa 613320183 auqacuy 147847601 u 58500993 aesc 461860441 1 x 613320183 3 su 613320183 g 147847601 a 750104225 3 saic 589460253 i 147847601 s 3910501 1 ioqxgke 812288287 5 a 878568205 a 147847601 c 1474279...
output:
Case #1: 1 Case #2: 1 Case #3: 1 Case #4: -1 Case #5: 3 Case #6: 1 Case #7: 2 Case #8: 2 Case #9: 1 Case #10: -1 Case #11: 2 Case #12: 2 Case #13: 3 Case #14: 1 Case #15: 1 Case #16: 1 Case #17: 1 Case #18: 1 Case #19: 1 Case #20: 1 Case #21: 1 Case #22: 1 Case #23: 1 Case #24: 1 Case #25: 1 Case #2...
result:
wrong answer 5th lines differ - expected: 'Case #5: 4', found: 'Case #5: 3'