QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#417715#8720. BFS 序 0Larunatrecy#WA 134ms50276kbC++142.0kb2024-05-22 21:11:282024-05-22 21:11:28

Judging History

This is the latest submission verdict.

  • [2024-05-22 21:11:28]
  • Judged
  • Verdict: WA
  • Time: 134ms
  • Memory: 50276kb
  • [2024-05-22 21:11:28]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
template <typename T>inline void read(T &x)
{
    x=0;char c=getchar();bool f=0;
    for(;c<'0'||c>'9';c=getchar())f|=(c=='-');
    for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+(c-'0');
    x=(f?-x:x);
}
const int N = 5e5+7;
int fa[N][20],dep[N];
int n,m,q,seq[N];
vector<int> used;
vector<int> G[N];
int deg[N];
int lca(int x,int y)
{
    if(dep[x]<dep[y])swap(x,y);
    for(int k=19;k>=0;k--)
    if(dep[fa[x][k]]>=dep[y])
    x=fa[x][k];
    if(x==y)return x;
    for(int k=19;k>=0;k--)
    if(fa[x][k]!=fa[y][k])
    {
        x=fa[x][k];
        y=fa[y][k];
    }
    return fa[x][0];
}
int jump(int x,int y)
{
    for(int k=19;k>=0;k--)
    if(dep[fa[x][k]]>dep[y])
    x=fa[x][k];
    return x;
}
bool solve()
{
    for(int x:used)deg[x]=0,G[x].clear();
    used.clear();
    for(int i=1;i+1<=m;i++)
    {
        int x=seq[i],y=seq[i+1];
        if(dep[x]>dep[y])return 0;
        if(x==y)return 0;
        if(dep[x]!=dep[y])continue;
        int o=lca(x,y);
        x=jump(x,o);
        y=jump(y,o);
        G[x].push_back(y);
        deg[y]++;
        used.push_back(x);
        used.push_back(y);
    }
    queue<int> que;
    used.erase(unique(used.begin(),used.end()),used.end());
    for(int x:used)
    if(!deg[x])
    {
        que.push(x);
    }
    while(!que.empty())
    {
        int x=que.front();
        que.pop();
        for(int y:G[x])if(!(--deg[y]))
        que.push(y);
    }
    for(int x:used)if(deg[x])return 0;
    return 1;
}
int main()
{
    read(n);
    for(int i=2;i<=n;i++)
    {
        read(fa[i][0]);
        dep[i]=dep[fa[i][0]]+1;
        for(int k=1;fa[i][k-1];k++)
        fa[i][k]=fa[fa[i][k-1]][k-1];
    }
    read(q);
    while(q--)
    {
        read(m);
        for(int i=1;i<=m;i++)read(seq[i]);
        bool f=solve();
        if(f)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 134ms
memory: 50276kb

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
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
YES
NO
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
YES
NO
NO
NO
NO
NO
YES
YES
NO
NO
YES
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO
YES
NO
NO
YES
NO
YES
NO
NO
NO
YES
NO
NO...

result:

wrong answer expected YES, found NO [3rd token]