QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#424461 | #8720. BFS 序 0 | _mjj | TL | 1ms | 3820kb | C++20 | 2.2kb | 2024-05-29 10:55:33 | 2024-05-29 10:55:33 |
Judging History
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]] ++;
}
}
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]){
d[c] --;
if(d[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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3820kb
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
Time Limit Exceeded
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...