QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416635 | #8720. BFS 序 0 | Afterlife# | WA | 291ms | 129804kb | C++17 | 2.5kb | 2024-05-22 01:12:20 | 2024-05-22 01:12:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1e3+7;
int n,st[N],ed[N],dep[N],dc,rev[N];
vector<int> g[N];
int anc[N][21];
void dfs(int x,int f)
{
st[x]=++dc;
rev[dc]=x;
anc[x][0]=f;
for(int j=1;j<=20;j++)
anc[x][j]=anc[anc[x][j-1]][j-1];
for(auto v:g[x])
{
if(v==f)
continue;
dep[v]=dep[x]+1;
dfs(v,x);
}
ed[x]=dc;
}
int lca(int x,int y)
{
if(dep[x]<dep[y])
swap(x,y);
for(int i=20;i>=0;i--)
if(dep[x]-(1<<i)>=dep[y])
x=anc[x][i];
if(x==y)
return x;
for(int i=20;i>=0;i--)
if(anc[x][i]!=anc[y][i])
x=anc[x][i],y=anc[y][i];
return anc[x][0];
}
int fup(int a,int b)
{
for(int i=20;i>=0;i--)
if(dep[a]-(1<<i)>dep[b])
a=anc[a][i];
return a;
}
namespace SOLVE {
int m, i, a[N], q[N], t, tot;
bool vip[N], vis[N];
bool cmp(int x, int y) { return st[x] < st[y]; }
vector<int> g[N];
vector<int> h[N];
int deg[N];
void solve(vector<int> v,vector<int> seq) {
set<int> use;
bool ok=1;
for(int i=0;i+1<seq.size();i++)
{
int x=seq[i],y=seq[i+1];
if(dep[x]!=dep[y])
{
ok&=dep[x]<dep[y];
continue;
}
int z=lca(x,y);
x=fup(x,z),y=fup(y,z);
h[x].push_back(y);
use.insert(x);
use.insert(y);
++deg[y];
}
queue<int> q;
for(auto x:use)
{
if(!deg[x])
q.push(x);
}
int cnt=0;
while(!q.empty())
{
int x=q.front();
q.pop();
++cnt;
for(auto v:h[x])
if(!--deg[v])
q.push(v);
}
ok&=cnt==use.size();
for(auto x:use)
deg[x]=0;
if(ok)
cout<<"Yes\n";
else
cout<<"No\n";
for (auto x : use)
vis[x] = vip[x] = 0, g[x].clear();
return;
}
} // namespace SOLVE
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=2;i<=n;i++)
{
int f;
cin>>f;
// f=rand()%n+1;
g[f].push_back(i);
}
dfs(1,0);
int m;
cin>>m;
while(m--)
{
int k;
cin>>k;
vector<int> s;
while(k--)
{
int x;
cin>>x;
// x=1;
s.push_back(x);
}
SOLVE::solve(s,s);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 12ms
memory: 85444kb
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: 291ms
memory: 129804kb
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 No No Yes Yes Yes No No No No No Yes No Yes No No No No Yes Yes No No No Yes No No No No No No No No No Yes No No 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 Yes No Yes No Yes Yes No Yes No No No Yes No ...
result:
wrong answer expected YES, found NO [14th token]