QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416273 | #8720. BFS 序 0 | podys | WA | 247ms | 87592kb | C++20 | 1.7kb | 2024-05-21 18:42:19 | 2024-05-21 18:42:20 |
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[300005];
vector<int> to[300005];
set<int> test[300005],node;
bool flag;
void dfs(int root,int 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;
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(){
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();
}
node.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);
}
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: 24072kb
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: 247ms
memory: 87592kb
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 Yes No Yes No Yes Yes Yes No No No No No No No Yes No No No No Yes Yes No No No Yes No No No No No No No Yes No Yes No Yes 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 No No Yes No No Yes No Yes No No No Yes No No...
result:
wrong answer expected YES, found NO [3rd token]