QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#863451#9716. Code a TrieIllusionaryDominance#WA 1ms3840kbC++203.1kb2025-01-19 17:18:012025-01-19 17:18:15

Judging History

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

  • [2025-01-19 17:18:15]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3840kb
  • [2025-01-19 17:18:01]
  • 提交

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];

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

void solve(int cas) {
    for (int i = 1; i <= N; i ++) {
        node[i].clear();
    }
    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);
    memset(val, 0, sizeof(int) * (tot + 1));
    int ans = tot + 1;
    for (int i = 1; i <= M; i ++) {
        int lca = 0;
        for (auto u : node[i]) {
            lca = Lca(u, lca);
        }
        if (cnt[lca] > 1) {
            if ((int)node[i].size() == 1) {
                val[lca] = 1;
                continue;
            }
            cout << "-1\n";
            return ;
        }
    }
    for (int i = 1; i <= M; i ++) {
        int lca = 0;
        for (auto u : node[i]) {
            lca = Lca(u, lca);
        }
        if (cnt[lca] == 1) {
            while (lca && cnt[fa[lca]] == 1) lca = fa[lca];
            if (lca && !val[fa[lca]]) {
                ans --;
                val[fa[lca]] = 1;
            }
            ans -= sz[lca] - 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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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: 4
Case #6: 1
Case #7: 3
Case #8: 3
Case #9: 1
Case #10: -1
Case #11: 3
Case #12: -1
Case #13: -1
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: 2
Case #23: 1
Case #24: 1
Case #25: 1
Case ...

result:

wrong answer 13th lines differ - expected: 'Case #13: 4', found: 'Case #13: -1'