QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#424476#8720. BFS 序 0_mjjWA 88ms32064kbC++202.2kb2024-05-29 11:06:552024-05-29 11:06:56

Judging History

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

  • [2024-05-29 11:06:56]
  • 评测
  • 测评结果:WA
  • 用时:88ms
  • 内存:32064kb
  • [2024-05-29 11:06:55]
  • 提交

answer

#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin() , v.end()
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int , int> PII;

const int N = 3e5 + 5;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;

vector<int> q[N];
int d[N];
vector<int> e[N];
int in[N];

void dfs(int u , int fa){
    d[u] = d[fa] + 1;
    for(auto c : q[u]){
        if(c == fa)continue;
        dfs(c , u);
    }
}

void solve(){
    int n;
    cin >> n;
    for(int i = 1 ; i < n ; i ++){
        int x;
        cin >> x;
        q[x].push_back(i + 1);
    }
    dfs(1 , 0);
    int q;
    cin >> q;
    while(q --){
        int m;
        cin >> m;
        vector<int> ss;
        bool ok = 1;
        for(int i = 1 ; i <= m ; i ++){
            int x;
            cin >> x;
            ss.push_back(x);
        }
        int mx = 0;
        for(auto c : ss){
            if(d[c] < mx)ok = 0;
            mx = max(mx , d[c]);
        }
        if(!ok){
            cout << "No\n";
            for(int i = 0 ; i < m ; i ++)e[ss[i]].clear() , in[ss[i]] = 0;
            continue;
        }
        for(int i = 0 ; i < m ; i ++){
            int j = i;
            while(j + 1 < m && d[j + 1] == d[i])j ++;
            for(int k = i ; k < j ; k ++){
                e[ss[k]].push_back(ss[k + 1]);
                in[ss[k + 1]] ++;
            }
            i = j;
        }
        queue<int> qq;
        for(int i = 0 ; i < m ; i ++){
            if(in[ss[i]] == 0)qq.push(ss[i]);
        }
        while(qq.size()){
            auto t = qq.front();
            qq.pop();
            for(auto c : e[t]){
                in[c] --;
                if(in[c] == 0){
                    qq.push(c);
                }
            }
        }
        for(int i = 0 ; i < m ; i ++){
            if(in[ss[i]])ok = 0;
        }
        if(!ok)cout << "No\n";
        else cout << "Yes\n";
        for(int i = 0 ; i < m ; i ++)e[ss[i]].clear() , in[ss[i]] = 0;
    }
}

signed main(){
    ios;
    int T = 1;
    // cin >> T;
    while(T --){
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3560kb

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: 88ms
memory: 32064kb

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:

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
...

result:

wrong answer expected NO, found YES [1st token]