QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416261 | #8720. BFS 序 0 | podys | WA | 284ms | 94832kb | C++20 | 1.8kb | 2024-05-21 18:28:52 | 2024-05-21 18:28:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
int fa[300005][20],dep[300005],a[500005],in[500005];
vector<int> to[300005];
set<int> test[300005],node;
bool flag;
void dfs(int root,int f){
// fa[root][0]=f;
// dep[root]=dep[f]+1;
for (int i=1;i<=19;i++) {
fa[root][i] = fa[fa[root][i-1]][i-1];
}
for (auto x:to[root]) {
if (x == f) continue;
dep[x]=dep[root]+1;
dfs(x, root);
}
}
void lca(int x, int y) {
for (int j = 19; j >= 0; j--) {
if (fa[x][j] != fa[y][j]) {
x = fa[x][j];
y = fa[y][j];
}
}
test[x].insert(y);
in[y]++;
node.insert(x);node.insert(y);
}
bool solve(){
node.clear();
flag=1;
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=1;i<n;i++){
if(a[i-1]==a[i]) flag=0;
if(dep[a[i-1]]>dep[a[i]]) flag=0;
}
if(!flag) return 0;
for(int i=1;i<n;i++){
if(dep[a[i-1]]<dep[a[i]]) continue;
lca(a[i-1],a[i]);
}
deque<int> que;
for(auto x:node){
if(!in[x]) que.push_back(x);
}
while(!que.empty()){
int x = que.front();
que.pop_front();
for (auto y : test[x]) {
in[y]--;
if (!in[y]) que.push_back(y);
}
}
for(auto x:node){
if(in[x]) flag=0,in[x]=0,test[x].clear();
}
return flag;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,q;
cin>>n;
for(int i=2;i<=n;i++){
cin>>fa[i][0];
to[fa[i][0]].push_back(i);
}
dep[1]=1;
dfs(1,0);
cin>>q;
while(q--) cout<<(solve()?"Yes":"No")<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 27564kb
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: 284ms
memory: 94832kb
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 No No No No No No No No Yes 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 Yes Yes No No Yes No No No No No No Yes No No No No No No No No Yes No No No Yes No No Yes No No No No Yes No No No Yes No No No No N...
result:
wrong answer expected YES, found NO [3rd token]