QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#424459 | #8720. BFS 序 0 | _mjj | TL | 0ms | 3884kb | C++20 | 2.3kb | 2024-05-29 10:54:26 | 2024-05-29 10:54:26 |
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;
set<int> s;
vector<int> ss;
bool ok = 1;
for(int i = 1 ; i <= m ; i ++){
int x;
cin >> x;
if(s.count(x))ok = 0;
ss.push_back(x);
}
if(!ok){
cout << "No\n";
continue;
}
int mx = 0;
for(auto c : ss){
if(d[c] < mx)ok = 0;
mx = max(mx , d[c]);
}
if(!ok){
cout << "No\n";
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3884kb
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...