QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416631 | #8720. BFS 序 0 | Afterlife# | RE | 738ms | 150092kb | C++17 | 4.9kb | 2024-05-22 00:55:28 | 2024-05-22 00:55:28 |
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];
}
set<vector<int> >SQ;
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;
int pos[N];
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) {
// bool cok=0;
// for(auto s:SQ)
// {
// auto it=seq.begin();
// for(auto p:s)
// {
// if(it!=seq.end()&&*it==p)
// it++;
// }
// if(it==seq.end())
// {
// cok=1;
// break;
// }
// }
for(int i=0;i+1<seq.size();i++)
{
int x=seq[i],y=seq[i+1];
if(dep[x]!=dep[y])
continue;
int z=lca(x,y);
v.push_back(fup(x,z));
v.push_back(fup(y,z));
}
int la=-1;
bool ok=1;
for(auto x:seq)
{
if(dep[x]<la)
ok=0;
la=dep[x];
}
if(!ok)
{
cout<<"No\n";
return;
}
if (!v.size())
return;
vis[1] = vip[1] = a[1] = 1;
m = v.size();
for (tot = ++m, i = 2; i <= m; i++)
a[i] = v.back(), v.pop_back(), vis[a[i]] = vip[a[i]] = 1;
sort(a + 1, a + m + 1, cmp);
int x;
for (i = 1; i < m; i++)
if (!vis[x = lca(a[i], a[i + 1])])
vis[a[++tot] = x] = 1;
vector<int> use;
m = tot, sort(a + 1, a + m + 1, cmp);
for (q[t = 1] = 1, i = 2; i <= m; q[++t] = a[i++]) {
while (st[a[i]] < st[q[t]] || ed[a[i]] > ed[q[t]]) // dfs order
t--;
if (q[t] != a[i])
g[q[t]].push_back(a[i]);
use.push_back(q[t]);
use.push_back(a[i]);
}
sort(use.begin(),use.end());
use.erase(unique(use.begin(),use.end()),use.end());
for(auto x:use)
h[x].clear(),h[x+n].clear(),deg[x]=0,deg[x+n]=0;
for(auto x:use)
{
h[x+n].push_back(x);
++deg[x];
for(auto y:g[x])
{
h[y].push_back(x);
++deg[x];
h[x+n].push_back(y+n);
++deg[y+n];
}
}
for(int i=0;i+1<seq.size();i++)
{
int x=seq[i],y=seq[i+1];
if(dep[x]!=dep[y])
continue;
int z=lca(x,y);
x=fup(x,z),y=fup(y,z);
h[x].push_back(y+n);
++deg[y+n];
}
for(int i=0;i<seq.size();i++)
pos[seq[i]]=0;
queue<int> q;
for(auto x:use)
{
if(!deg[x])
q.push(x);
if(!deg[x+n])
q.push(x+n);
}
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()*2;
// if(ok!=cok)
// {
// cerr<<"ALERT"<<endl;
// exit(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()%(i-1)+1;
g[f].push_back(i);
}
// vector<int> p(n);
// iota(p.begin(),p.end(),0);
// do {
// vector<int> s;
// queue<int> Q;
// Q.push(1);
// while(!Q.empty())
// {
// int x=Q.front();
// s.push_back(x);
// Q.pop();
// sort(g[x].begin(),g[x].end(),[&](const int &a,const int &b){return p[a-1]<p[b-1];});
// for(auto y:g[x])
// Q.push(y);
// }
// SQ.insert(s);
// }while(next_permutation(p.begin(),p.end()));
dfs(1,0);
int m;
cin>>m;
while(m--)
{
int k;
// k=rand()%n+1;
cin>>k;
vector<int> s;
while(k--)
{
int x;
cin>>x;
// x=rand()%n+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: 20ms
memory: 91556kb
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: 719ms
memory: 143992kb
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: 731ms
memory: 150092kb
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: 738ms
memory: 147948kb
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: 19ms
memory: 89552kb
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: 7ms
memory: 89564kb
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: 13ms
memory: 89784kb
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: 13ms
memory: 89600kb
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: -3
Extra Test Failed : Runtime Error on 2
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...