QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416246#8720. BFS 序 0podysWA 343ms105604kbC++201.7kb2024-05-21 18:14:152024-05-21 18:14:16

Judging History

你现在查看的是最新测评结果

  • [2024-05-21 18:14:16]
  • 评测
  • 测评结果:WA
  • 用时:343ms
  • 内存:105604kb
  • [2024-05-21 18:14:15]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
int fa[300005][25],dep[300005],a[500005],in[500005];
vector<int> to[300005];
set<int> test[300005];
set<int> node;
bool flag;
void dfs(int root,int f){
    fa[root][0]=f;
    dep[root]=dep[f]+1;
    for (int i=1;i<=24;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 = 24; 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);
    }
    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: 0ms
memory: 25620kb

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: 343ms
memory: 105604kb

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]