QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416259#8720. BFS 序 0podysWA 296ms93376kbC++202.2kb2024-05-21 18:24:222024-05-21 18:24:23

Judging History

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

  • [2024-05-21 18:24:23]
  • 评测
  • 测评结果:WA
  • 用时:296ms
  • 内存:93376kb
  • [2024-05-21 18:24:22]
  • 提交

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];
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<=19;i++) {
        fa[root][i] = fa[fa[root][i-1]][i-1];
    }
    for (auto x:to[root]) {
        if (x == f) continue;
        dfs(x, root);
    }
}
int lk(int x, int y) {
	for (int j = 19; j >= 0; j--)
		if (dep[fa[x][j]] > dep[y]) x = fa[x][j];
	return x;
}

int 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];
        }
    }
    return fa[x][0];
}

// 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;
        int lc = lca(a[i-1],a[i]);
        int u = lk(a[i-1], lc), v = lk(a[i], lc);
        test[u].insert(v);
        in[v]++;
        node.insert(u);node.insert(v);
    }
    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: 23908kb

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: 296ms
memory: 93376kb

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]