QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#419157 | #8720. BFS 序 0 | tzxydby# | WA | 2138ms | 92544kb | C++20 | 2.0kb | 2024-05-23 18:18:39 | 2024-05-23 18:18:40 |
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;
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]);
for(int i=1;i<=m;i++)
d[i]=dep[a[i]];
ok=1;
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();
}
puts(ok?"Yes":"No");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 11960kb
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: 2138ms
memory: 92544kb
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 Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes ...
result:
wrong answer expected NO, found YES [1st token]