QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#417720#8720. BFS 序 0Larunatrecy#AC ✓162ms69864kbC++142.1kb2024-05-22 21:16:322024-05-22 21:16:35

Judging History

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

  • [2024-05-22 21:16:35]
  • 评测
  • 测评结果:AC
  • 用时:162ms
  • 内存:69864kb
  • [2024-05-22 21:16:32]
  • 提交

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 = 1e6+7;
int fa[N][20],dep[N];
int n,m,q,seq[N];
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;
}
int used[N],tot=0;
bool solve()
{
    for(int i=1;i<=tot;i++)
    {
        int x=used[i];
        deg[x]=0;
        G[x].clear();
    }
    tot=0;
    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[++tot]=x;
        used[++tot]=y;
    }
    sort(used+1,used+tot+1);
    tot=unique(used+1,used+tot+1)-used-1;
    queue<int> que;
    for(int i=1;i<=tot;i++)
    {
        int x=used[i];
        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 i=1;i<=tot;i++)
    {
        int x=used[i];
        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;
}



这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 162ms
memory: 63800kb

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: 151ms
memory: 69864kb

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: 149ms
memory: 65984kb

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: 0
Accepted
time: 3ms
memory: 34360kb

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:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
YES
NO
NO
NO
NO
NO
NO
...

result:

ok 10000 token(s): yes count is 1058, no count is 8942

Test #6:

score: 0
Accepted
time: 3ms
memory: 34268kb

input:

9
1 2 1 2 2 4 2 6
10000
6 5 1 4 3 7 8
14 6 8 7 9 7 5 1 8 3 6 3 8 5 2
2 6 9
17 7 8 6 5 2 1 5 5 9 3 4 1 5 2 7 2 5
3 8 6 5
5 3 9 1 6 8
8 3 5 8 7 9 4 6 2
4 8 9 6 5
7 4 7 2 9 3 8 1
6 1 4 9 5 6 2
4 7 5 4 3
5 9 5 2 1 3
4 4 5 7 3
3 9 1 8
4 4 2 8 6
26 7 4 7 6 1 7 9 4 5 5 8 9 8 2 4 8 9 6 2 8 6 4 5 5 4 4
45 3 ...

output:

NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
N...

result:

ok 10000 token(s): yes count is 1492, no count is 8508

Test #7:

score: 0
Accepted
time: 7ms
memory: 34268kb

input:

8
1 1 2 4 2 4 6
10000
5 2 6 6 2 1
3 7 3 8
2 8 1
5 7 5 1 2 7
3 2 3 5
3 8 4 3
5 8 2 4 5 3
4 3 2 1 8
6 6 8 4 5 3 1
6 3 1 2 7 4 6
4 8 6 4 3
5 4 7 6 8 5
11 2 1 6 5 2 7 1 3 3 5 4
4 6 1 7 2
7 6 2 7 8 4 1 3
3 4 6 7
3 7 1 6
38 3 4 6 6 3 4 8 1 5 3 1 8 8 6 2 3 6 7 8 8 4 2 3 7 3 3 6 7 7 5 7 5 8 8 5 4 5 7
1 8
5 ...

output:

NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
...

result:

ok 10000 token(s): yes count is 1568, no count is 8432

Test #8:

score: 0
Accepted
time: 0ms
memory: 36392kb

input:

7
1 2 3 1 2 5
10000
5 1 7 3 5 6
5 5 3 2 7 4
4 5 2 6 4
12 3 1 5 5 6 6 3 5 3 5 3 3
11 2 3 2 6 7 3 6 6 3 3 3
2 3 2
6 1 3 4 2 6 7
3 2 7 1
6 5 2 7 3 4 1
27 2 5 6 1 7 1 7 1 4 6 7 1 6 2 1 7 1 3 4 4 7 7 2 6 4 4 3
2 5 6
1 7
3 3 1 4
4 1 7 3 4
3 4 2 1
4 4 1 6 7
4 6 2 7 4
4 5 1 6 4
1 3
5 1 2 3 5 7
5 2 3 7 6 5
1...

output:

NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
YES
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
YES
NO
YES
YES
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
YES
YES
YES
YES
YES
YES
YE...

result:

ok 10000 token(s): yes count is 2205, no count is 7795

Extra Test:

score: 0
Extra Test Passed