QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#419164 | #8720. BFS 序 0 | tzxydby# | WA | 169ms | 53108kb | C++20 | 2.1kb | 2024-05-23 18:20:58 | 2024-05-23 18:21:02 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=300005;
int n,m,q,fa[N][20],dep[N],a[N],d[N],dfn[N],low[N],tim,in[N],st[N],tp,ok,vis[N];
vector<int>e[N],g[N],p;
void dfs(int u,int f)
{
fa[u][0]=f;
dep[u]=dep[f]+1;
for(auto v:e[u])
if(v!=f)
dfs(v,u);
}
int lca(int u,int v)
{
if(dep[u]<dep[v])
swap(u,v);
for(int i=18;i>=0;i--)
if(dep[fa[u][i]]>=dep[v])
u=fa[u][i];
if(u==v)
return u;
for(int i=18;i>=0;i--)
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
return fa[u][0];
}
int cal(int u,int v)
{
for(int i=18;i>=0;i--)
if(dep[fa[u][i]]>dep[v])
u=fa[u][i];
return u;
}
void tarjan(int u)
{
dfn[u]=low[u]=++tim;
st[++tp]=u,in[u]=1;
for(auto v:g[u])
{
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(in[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int v;
do
{
v=st[tp--];
in[v]=0;
if(v!=u)
ok=0;
}while(v!=u);
}
}
int main()
{
scanf("%d",&n);
for(int i=2;i<=n;i++)
{
int x;
scanf("%d",&x);
e[x].push_back(i);
}
dfs(1,0);
for(int j=1;j<=18;j++)
for(int i=1;i<=n;i++)
fa[i][j]=fa[fa[i][j-1]][j-1];
scanf("%d",&q);
while(q--)
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d",&a[i]);
ok=1;
for(int i=1;i<=m;i++)
{
if(vis[a[i]])
ok=0;
vis[a[i]]=1;
}
for(int i=1;i<=m;i++)
vis[a[i]]=0;
for(int i=1;i<=m;i++)
d[i]=dep[a[i]];
for(int i=1;i<m;i++)
if(d[i]>d[i+1])
ok=0;
if(!ok)
{
puts("No");
continue;
}
for(int i=1;i<=m;i++)
{
int j=i;
while(j<m&&d[j+1]==d[i])
j++;
for(int k=i;k<j;k++)
{
int l=lca(a[i],a[i+1]);
int u=cal(a[i],l),v=cal(a[i],l);
p.push_back(u);
p.push_back(v);
g[u].push_back(v);
}
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()),p.end());
for(auto u:p)
if(!dfn[u])
tarjan(u);
if(!ok)
break;
for(auto u:p)
{
g[u].clear();
dfn[u]=low[u]=in[u]=0;
}
tim=tp=0;
p.clear();
i=j;
}
puts(ok?"Yes":"No");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13976kb
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: 169ms
memory: 53108kb
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 Yes No No ...
result:
wrong answer expected NO, found YES [87th token]