QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#602856#8720. BFS 序 0UESTC_DECAYALI#WA 168ms46212kbC++202.6kb2024-10-01 13:11:042024-10-01 13:11:06

Judging History

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

  • [2024-10-01 13:11:06]
  • 评测
  • 测评结果:WA
  • 用时:168ms
  • 内存:46212kb
  • [2024-10-01 13:11:04]
  • 提交

answer

#include <bits/stdc++.h>

std::vector <int> G[300005];
int deg[300005];

void work() {
    int n; std::cin >> n;
    std::vector<int> pa(n, 0);
    std::vector<std::vector<int>> ch(n), loop(n);
    std::vector<int> dfn(n), dep(n);
    for(int i = 1; i < n; ++i) std::cin >> pa[i], pa[i]--, ch[pa[i]].emplace_back(i);

    int O = 0;
    auto dfs = [&](auto dfs, int cur) -> void {
        dfn[cur] = O++;
        loop[cur].emplace_back(pa[cur]);
        while(loop[cur].back() == 0) {
            if(loop[loop[cur].back()].size() <= loop[cur].size()) break;
            loop[cur].emplace_back(loop[loop[cur].back()][loop[cur].size() - 1]);
        }
        for(auto ch: ch[cur]) dep[ch] = dep[cur] + 1, dfs(dfs, ch);
    };

    auto lca = [&](int a, int b) -> int {
        if(dep[a] < dep[b]) std::swap(a, b);
        for(int i = 20; ~i; --i) if((dep[a] - dep[b]) & (1 << i)) a = loop[a][i];
        if(a == b) return a;
        for(int i = 20; ~i; --i) if(loop[a].size() > i && loop[a][i] != loop[b][i])
            a = loop[a][i], b = loop[b][i];
        return loop[a][0];
    };

    auto jump = [&](int &x, int step) {
        assert(dep[x] >= step);
        for (int i=20;i>=0;--i) if ((step>>i)&1) x=loop[x][i];
    };

    dep[0] = 0;
    dfs(dfs, 0);

    int q; std::cin >> q;

    auto query = [&]() -> bool {
        int m; std::cin >> m;
        std::vector<int> b(m);
        for(auto &b: b) std::cin >> b, b--;
        std::vector <int> ext;
        for (int i=0;i+1<m;++i)
        if (dep[b[i+1]]<dep[b[i]]||b[i]==b[i+1]) return false;
        for (int i=0;i+1<m;++i)
        {
            int x=b[i],y=b[i+1];
            if (dep[x]!=dep[y]) continue;
            int z=lca(x,y);
            jump(x,dep[x]-dep[z]-1); jump(y,dep[y]-dep[z]-1);
            ext.push_back(x); ext.push_back(y);
            G[x].push_back(y); ++deg[y];
        }
        std::sort(ext.begin(),ext.end());
        ext.erase(std::unique(ext.begin(),ext.end()),ext.end());
        std::queue <int> Q; int cnt=0;
        for (auto x:ext) if (!deg[x]) Q.push(x),++cnt;
        while (!Q.empty())
        {
            int now=Q.front(); Q.pop();
            for (auto to:G[now])
            if (!--deg[to]) Q.push(to),++cnt;
        }
        for (auto x:ext) G[x].clear(),deg[x]=0;
        return cnt==(int)ext.size();
    };

    while(q--) std::cout << (query() ? "Yes\n" : "No\n");
    return ;
}

int main() {
    // freopen("H.in","r",stdin);
    std::ios::sync_with_stdio(false);
    int T = 1; while(T--) work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3796kb

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: 168ms
memory: 46212kb

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:

wrong answer expected NO, found YES [176th token]