QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#417442 | #8720. BFS 序 0 | grass8cow# | WA | 125ms | 60960kb | C++17 | 1.8kb | 2024-05-22 18:52:27 | 2024-05-22 18:52:28 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
vector<int>g[501000];
int f[500100][20],d[500100],dfn[501000],sz[501000];
#define pb push_back
void dfs(int x){
sz[x]=1,dfn[x]=++dfn[0];
for(int v:g[x]){
f[v][0]=x,d[v]=d[x]+1;
for(int i=1;i<20;i++)f[v][i]=f[f[v][i-1]][i-1];
dfs(v),sz[x]+=sz[v];
}
}
int lca(int u,int v){
if(d[u]<d[v])swap(u,v);int a=d[u]-d[v];
for(int i=19;i>=0;i--)if((a>>i)&1)u=f[u][i];if(u==v)return u;
for(int i=19;i>=0;i--)if(f[u][i]!=f[v][i])u=f[u][i],v=f[v][i];
return f[u][0];
}
int zj(int x,int t){
if(dfn[x]<=dfn[t]&&dfn[t]<dfn[x]+sz[x]){
for(int i=19;i>=0;i--)
if(f[t][i]&&d[f[t][i]]>d[x])t=f[t][i];
return t;
}
return f[x][0];
}
#define pb push_back
int n,tp[501000],S,a[500100],rk[501000],b[501000],ru[501000];
vector<int>cj[501000];
int main(){
scanf("%d",&n);
for(int i=2,f;i<=n;i++)scanf("%d",&f),g[f].pb(i);
dfs(1);
int q;scanf("%d",&q);
while(q--){
scanf("%d",&S);
for(int i=1;i<=S;i++)scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+S+1);
bool fl=1;
for(int i=1;i<S;i++)fl&=(d[a[i]]<=d[a[i+1]]),fl&=(b[i]!=b[i+1]);
if(!fl){puts("No");continue;}
int e=0;
for(int i=1;i<S;i++)if(d[a[i]]==d[a[i+1]]){
int lc=lca(a[i],a[i+1]),u=zj(a[i],lc),v=zj(a[i+1],lc);
if(tp[u]!=q)tp[u]=q,rk[u]=++e,ru[e]=0,cj[e].clear();
if(tp[v]!=q)tp[v]=q,rk[v]=++e,ru[e]=0,cj[e].clear();
cj[rk[u]].pb(rk[v]),ru[rk[v]]++;
}
queue<int>z;
for(int i=1;i<=e;i++)if(!ru[i])z.push(i);
int vc=0;
while(!z.empty()){
int u=z.front();z.pop();vc++;
for(int v:g[u])if(!(--ru[v]))z.push(v);
}
puts((vc==e)?"Yes":"No");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 28264kb
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: 125ms
memory: 60960kb
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 No 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]