QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#795678#8720. BFS 序 0CalculateloveWA 107ms52564kbC++142.8kb2024-11-30 23:05:442024-11-30 23:05:44

Judging History

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

  • [2024-11-30 23:05:44]
  • 评测
  • 测评结果:WA
  • 用时:107ms
  • 内存:52564kb
  • [2024-11-30 23:05:44]
  • 提交

answer

#include <bits/stdc++.h>

const int N = 500100;

int n, Q;

int dep[N];
int anc[19][N];

int lca(int x, int y) {
    if (dep[x] > dep[y]) std::swap(dep[x], dep[y]);
    for (int i = 18; i >= 0; i --)
        if (dep[x] <= dep[y] - (1 << i)) y = anc[i][y];
    if (x == y) return x;
    for (int i = 18; i >= 0; i --)
        if (anc[i][x] ^ anc[i][y]) x = anc[i][x], y = anc[i][y];
    return anc[0][x];
}

int jump(int x, int y) {
    for (int i = 18; i >= 0; i --)
        if (dep[anc[i][x]] > dep[y]) x = anc[i][x];
    return x;
}

int m, seq[N];

bool exist[N];
int crulen, cru[N];

bool insert(int x) {
    if (exist[x]) {
        return 0;
    } else {
        exist[x] = 1, cru[++ crulen] = x;
        return 1;
    }
}

int tot, head[N], ver[N * 2], Next[N * 2], deg[N];
void add_edge(int u, int v) {
    ver[++ tot] = v;    Next[tot] = head[u];    head[u] = tot;    deg[v] ++;
}

int main() {
    scanf("%d", &n);

    for (int i = 2; i <= n; i ++)
        scanf("%d", &anc[0][i]), dep[i] = dep[anc[0][i]] + 1;
    
    for (int j = 1; j <= 18; j ++)
        for (int i = 1; i <= n; i ++)
            anc[j][i] = anc[j - 1][anc[j - 1][i]];

    scanf("%d", &Q);

    while (Q --) {
        tot = 0;
        for (int i = 1; i <= crulen; i ++) exist[cru[i]] = 0, head[cru[i]] = 0, deg[cru[i]] = 0;
        crulen = 0;

        scanf("%d", &m);

        for (int i = 1; i <= m; i ++)
            scanf("%d", &seq[i]);
        
        bool flag = 1;

        for (int i = 1; i <= m; i ++)
            if (!insert(seq[i])) flag = 0;
        
        if (!flag) {
            puts("No");
            continue;
        }

        int d = dep[seq[1]];
        for (int i = 2; i <= m; i ++)
            if (d <= dep[seq[i]]) d = dep[seq[i]];
            else flag = 0;

        if (!flag) {
            puts("No");
            continue;
        }

        for (int st = 1, ed; st <= m; st = ed + 1) {
            ed = st;
            while (ed < m && dep[seq[ed + 1]] == dep[seq[ed]]) ed ++;

            for (int i = st; i < ed; i ++) {
                int x = seq[i], y = seq[i + 1], z = lca(x, y);
                int fx = jump(x, dep[x] - dep[z] - 1), fy = jump(y, dep[y] - dep[z] - 1);

                insert(fx), insert(fy);
                add_edge(fx, fy);
            }
        }

        std::queue<int> q;
        for (int i = 1; i <= crulen; i ++)
            if (deg[cru[i]] == 0) q.push(cru[i]);
        
        int t = 0;
        while (q.size()) {
            int u = q.front(); q.pop();
            t ++;

            for (int i = head[u]; i; i = Next[i]) {
                int v = ver[i];
                if (-- deg[v] == 0) q.push(v);
            }
        }

        puts(t == crulen ? "Yes" : "No");
    }

    return 0;
}

详细

Test #1:

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

input:

6
1 1 3 2 4
10
4 3 6 2 5
1 4
3 2 4 5
5 2 5 4 6 3
3 1 4 2
3 5 6 3
5 4 5 2 6 1
4 4 3 2 5
4 4 6 2 3
3 3 2 6

output:

No
Yes
Yes
No
No
No
No
No
No
Yes

result:

ok 10 token(s): yes count is 3, no count is 7

Test #2:

score: -100
Wrong Answer
time: 107ms
memory: 52564kb

input:

300000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
Yes
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
No...

result:

wrong answer expected YES, found NO [2nd token]