QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#605618 | #8551. DFS Order 5 | ucup-team4153 | WA | 26ms | 9488kb | C++17 | 2.5kb | 2024-10-02 18:11:47 | 2024-10-02 18:11:51 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,q,k;
vector<int>E[N];
int sz[N],fa[N],dep[N];
int a[N];
int jp[N][20];
void dfs(int u,int f){
sz[u]=1;
jp[u][0]=fa[u]=f;
dep[u]=dep[f]+1;
for(auto v:E[u]){
if(v==f)continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
bool check(int u,int pos,int cnt){
if(cnt==1 || pos==k)return true;
for(int l=pos+1;l<min(pos+cnt,k+1);l+=sz[a[l]]){
if(fa[a[l]]!=u)return false;
if(!check(a[l],l,sz[a[l]]))return false;
}
return true;
}
int jump(int u,int len){
for(int i=0;i<20;i++){
if((len>>i)&1){
u=jp[u][i];
}
}
return u;
}
bool nxt(int u,int v){
if(dep[u]<dep[v])return false;
int res=dep[u]-dep[v];
return jump(u,res)==v;
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>q;
for(int i=1;i<n;i++){
int u,v;cin>>u>>v;
E[u].push_back(v);
E[v].push_back(u);
}
dfs(1,0);
for(int j=1;j<20;j++){
for(int i=1;i<=n;i++){
jp[i][j]=jp[jp[i][j-1]][j-1];
}
}
while(q--){
cin>>k;
if(k>n){
while(k--){
int u;cin>>u;
break;
}
cout<<"No\n";
continue;
}
bool flag=true;
for(int i=1;i<=k;i++){
cin>>a[i];
if(a[i]==1 && i!=1){
flag=false;
}
}
if(!flag){
cout<<"No\n";
continue;
}
vector<int>b;
for(int l=1;l<=k;l+=sz[a[l]]){
b.push_back(fa[a[l]]);
flag&=check(a[l],l,sz[a[l]]);
if(l>1){
if(nxt(a[1],a[l])){
flag=false;
}
}
if(!flag)break;
}
if(!flag){
cout<<"No\n";
continue;
}
sort(a+1,a+k+1);
for(int i=2;i<=k;i++){
if(a[i]==a[i-1]){
flag=false;
break;
}
}
if(!flag){
cout<<"No\n";
continue;
}
int pre=b[0];
for(int i=1;i<b.size();i++){
if(!nxt(pre,b[i])){
flag=false;
break;
}
}
if(flag){
cout<<"Yes\n";
}else{
cout<<"No\n";
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9488kb
input:
6 7 1 2 1 3 2 4 3 5 2 6 2 4 1 2 4 2 2 4 3 2 4 4 2 4 5 2 4 6 6 1 2 6 4 3 5
output:
No No Yes No No Yes Yes
result:
ok 7 tokens
Test #2:
score: -100
Wrong Answer
time: 26ms
memory: 7028kb
input:
10 100000 7 2 1 7 7 10 8 6 8 7 1 3 4 5 9 5 5 8 8 8 9 7 2 8 1 6 1 4 8 3 5 2 6 7 10 3 9 9 1 1 1 8 10 3 2 9 3 8 7 3 7 5 6 2 8 5 9 1 6 3 4 6 2 1 3 5 8 9 2 4 9 1 3 2 1 5 5 8 5 1 7 9 10 5 2 9 2 6 4 10 6 3 8 3 4 5 8 2 8 4 9 4 10 1 2 4 3 3 6 3 1 3 6 1 1 6 8 3 1 3 7 3 2 3 9 1 5 4 3 7 8 10 9 4 2 3 10 2 5 4 3 ...
output:
No No No Yes No No No No Yes No No No No No No Yes No No No No No No No No No No No No No No No Yes No No No No No No Yes No No No No No Yes Yes No No No No No No No No No No No No No No No No No No No No No No No No Yes No No Yes Yes No No No No Yes No No No No No No No No No No No No No No No No N...
result:
wrong answer 206th words differ - expected: 'No', found: 'Yes'