QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#424724 | #8720. BFS 序 0 | _mjj | WA | 163ms | 82236kb | C++20 | 3.4kb | 2024-05-29 16:16:33 | 2024-05-29 16:16: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);
}
}
int qq[N] , depth[N];
int fa[N][20];
void bfs(int root){
memset(depth , 0x3f , sizeof depth);
depth[0] = 0 , depth[root] = 1;
qq[0] = root;
int tt = 0 , hh = 0;
while(hh <= tt){
int t = qq[hh ++];
for(auto c : q[t]){
int j = c;
if(depth[j] > depth[t] + 1){
depth[j] = depth[t] + 1;
qq[++ tt] = j;
fa[j][0] = t;
for(int k = 1 ; k <= 19 ; k ++)
fa[j][k] = fa[fa[j][k - 1]][k - 1];
}
}
}
}
int lca(int a , int b){
if(depth[a] < depth[b])swap(a , b);
for(int k = 19 ; k >= 0 ; k --)
if(depth[fa[a][k]] >= depth[b])
a = fa[a][k];
if(a == b)return a;
for(int k = 19 ; k >= 0 ; k --)
if(fa[a][k] != fa[b][k]){
a = fa[a][k];
b = fa[b][k];
}
return fa[a][0];
}
int get(int a , int b){
for(int i = 19 ; i >= 0 ; i --){
if(fa[a][i] && depth[fa[a][i]] > depth[b]){
a = fa[a][i];
}
}
return a;
}
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);
bfs(1);
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 ++){
int t = lca(ss[k] , ss[k + 1]);
int u = get(ss[k] , t) , v = get(ss[k + 1] , t);
e[u].push_back(v);
in[v] ++;
}
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9708kb
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: 163ms
memory: 82236kb
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 No No No Yes Yes Yes Yes Yes Yes No No Yes No Yes Yes Yes No Yes Yes Yes Yes No Yes Yes Yes Yes Yes Yes Yes No Yes No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Yes Yes Yes No Yes No Yes Yes No No Yes Yes Yes Yes Yes Yes Yes No Yes No Yes Yes Yes No Yes Yes No No Yes Yes Yes Yes Yes Yes No Yes N...
result:
wrong answer expected NO, found YES [1st token]