QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#417357#8720. BFS 序 0light_ink_dots#RE 191ms87936kbC++143.1kb2024-05-22 17:55:192024-05-22 17:55:19

Judging History

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

  • [2024-05-22 17:55:19]
  • 评测
  • 测评结果:RE
  • 用时:191ms
  • 内存:87936kb
  • [2024-05-22 17:55:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

int main() {
    static const int maxn = 500010, maxlg = 20;
    int n;
    scanf("%d", &n);
    static int fa[maxn];
    static vector<int> G[maxn];
    for (int i = 2; i <= n; i++) scanf("%d", fa + i), G[fa[i]].push_back(i);
    static int dfn[maxn], dep[maxn];
    static int lg[maxn], st[maxlg][maxn];
    static auto get = [](int u, int v) { return dep[u] < dep[v] ? u : v; };
    static vector<int> vec[maxn];
    function<void(int)> dfs = [&dfs](int u) {
        static int c;
        st[0][dfn[u] = ++c] = fa[u];
        for (int i : G[u]) dep[i] = dep[u] + 1, dfs(i), vec[u].push_back(dfn[i]);
        return;
    };
    dfs(1);
    for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
    for (int l = 1; l <= lg[n]; l++)
        for (int i = 1; i + (1 << l) - 1 <= n; i++)
            st[l][i] = get(st[l - 1][i], st[l - 1][i + (1 << (l - 1))]);
    auto lca = [](int u, int v) {
        if (u == v)
            return u;
        u = dfn[u], v = dfn[v];
        if (u > v)
            swap(u, v);
        int Lg = lg[v - (++u) + 1];
        return get(st[Lg][u], st[Lg][v - (1 << Lg) + 1]);
    };
    int q;
    scanf("%d", &q);
    while (q--) {
        int m;
        bool ans = true;
        scanf("%d", &m);
        static int a[maxn];
        for (int i = 1; i <= m; i++) scanf("%d", a + i);
        static bool vis[maxn];
        static vector<int> all;
        static vector<pair<int, int>> G[maxn];
        for (int i = 1; i + 1 <= m; i++) {
            if (dep[a[i]] != dep[a[i + 1]]) {
                ans &= dep[a[i]] < dep[a[i + 1]];
                continue;
            }
            int l = lca(a[i], a[i + 1]);
            if (!vis[l])
                vis[l] = true, all.push_back(l);
            int u = *prev(upper_bound(vec[l].begin(), vec[l].end(), dfn[a[i]]));
            int v = *prev(upper_bound(vec[l].begin(), vec[l].end(), dfn[a[i + 1]]));
            G[l].emplace_back(u, v);
        }
        for (int u : all) {
            vector<int> cur;
            static vector<int> g[maxn];
            static bool v[maxn];
            static int deg[maxn];
            for (auto e : G[u]) {
                if (!v[e.first])
                    v[e.first] = true, cur.push_back(e.first);
                if (!v[e.second])
                    v[e.second] = true, cur.push_back(e.second);
                g[e.first].push_back(e.second), deg[e.second]++;
            }
            int tot = 0;
            queue<int> q;
            for (int i : cur)
                if (!deg[i])
                    q.push(i);
            while (!q.empty()) {
                int u = q.front();
                q.pop(), tot++;
                for (int i : g[u])
                    if (!(--deg[i]))
                        q.push(i);
            }
            ans &= tot == cur.size();
            for (int i : cur) v[i] = false, deg[i] = 0, g[i].clear();
            G[u].clear(), vis[u] = false;
        }
        printf(ans ? "Yes\n" : "No\n");
        all.clear();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 50640kb

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: 0
Accepted
time: 191ms
memory: 87924kb

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
Yes
Yes
Yes
No
No
No
No
No
No
Yes
Yes
No
Yes
No
Yes
Yes
Yes
No
No
No
No
Yes
No
No
Yes
No
No
No
No
Yes
Yes
Yes
No
No
Yes
No
No
No
No
No
No
No
Yes
No
Yes
No
Yes
No
No
No
No
Yes
Yes
Yes
No
No
Yes
No
No
No
No
No
Yes
Yes
No
No
No
No
No
Yes
Yes
No
Yes
No
No
No
Yes
No
No
Yes
No
No
Yes
Yes
Yes
No
No
No
Y...

result:

ok 254 token(s): yes count is 89, no count is 165

Test #3:

score: 0
Accepted
time: 171ms
memory: 87872kb

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:

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

result:

ok 266 token(s): yes count is 102, no count is 164

Test #4:

score: 0
Accepted
time: 165ms
memory: 87936kb

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
Yes
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
No
Yes
No
No
No
No
No
No
No
Yes
Yes
No
No
No
No
No
No
Yes
No
No
No
Yes
Yes
No
No
No
Yes
Yes
No
Yes
No
Yes
No
Yes
No
Yes
No
Yes
No
No
Yes
No
No
No
No
No
No
No
Yes
Yes
No
Yes
No
No
No
No
No
No
Yes
Yes
No
Yes
Yes
No
No
No...

result:

ok 252 token(s): yes count is 87, no count is 165

Test #5:

score: -100
Runtime Error

input:

10
1 1 3 3 1 3 1 3 9
10000
4 3 5 6 9
5 3 1 4 9 6
5 6 4 3 5 7
28 8 3 4 3 1 6 3 5 8 7 2 4 4 4 2 7 5 9 2 4 10 8 4 10 3 4 5 7
3 5 2 9
6 4 7 2 3 9 5
5 9 2 5 6 1
12 3 5 8 8 1 3 5 8 4 5 9 1
29 3 9 9 1 5 8 5 5 6 10 8 8 4 8 4 2 8 10 5 7 3 7 1 8 4 7 5 1 6
7 5 9 8 3 2 1 6
4 4 1 9 5
22 10 9 5 4 5 8 10 3 2 1 10 ...

output:


result: