QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416266#8720. BFS 序 0podysWA 301ms90664kbC++201.8kb2024-05-21 18:37:312024-05-21 18:37:31

Judging History

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

  • [2024-05-21 18:37:31]
  • 评测
  • 测评结果:WA
  • 用时:301ms
  • 内存:90664kb
  • [2024-05-21 18:37:31]
  • 提交

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(){
    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);
        for (int j=1;j<=19;j++) {
            fa[i][j] = fa[fa[i][j-1]][j-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: 0ms
memory: 25340kb

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: 301ms
memory: 90664kb

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]