QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#648118 | #8551. DFS Order 5 | bruteforce_ | WA | 36ms | 3872kb | C++20 | 1.6kb | 2024-10-17 17:06:37 | 2024-10-17 17:06:49 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+7,inf=1e18,mod=998244353,S=20;
vector<vector<int>> e,fa;
vector<int> dep;
vector<bool> leaf;
void dfs(int u)
{
if(u!=1&&e[u].size()==1)
{
leaf[u]=1;
}
dep[u]=dep[fa[u][0]]+1;
for(auto v:e[u])
{
if(v==fa[u][0]) continue;
fa[v][0]=u;
dfs(v);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])
swap(x,y);
for(int i=S-1; i>=0; i--)
{
if(dep[fa[x][i]]>=dep[y])
{
x=fa[x][i];
}
}
if(x==y) return x;
for(int i=S-1; i>=0; i--)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][0];
}
void O_o()
{
int n,Q;
cin>>n>>Q;
e.assign(n+1,vector<int>());
for(int i=1; i<n; i++)
{
int x,y;
cin>>x>>y;
e[x].push_back(y);
e[y].push_back(x);
}
fa.assign(n+1,vector<int>(S));
dep.assign(n+1,0);
leaf.assign(n+1,0);
dfs(1);
for(int j=1; j<S; j++)
{
for(int i=1; i<=n; i++)
{
fa[i][j]=fa[fa[i][j-1]][j-1];
}
}
while(Q--)
{
int m;
cin>>m;
bool ok=1;
vector<int> p(m);
for(int i=0; i<m; i++)
{
cin>>p[i];
}
for(int i=1; i<m; i++)
{
int l=lca(p[i-1],p[i]);
if(l!=fa[p[i]][0])
{
ok=0;
break;
}
if(leaf[p[i-1]]!=1&&dep[p[i]]<=dep[p[i-1]])
{
ok=0;
break;
}
}
if(ok)
cout<<"Yes\n";
else
cout<<"No\n";
}
}
signed main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cout<<fixed<<setprecision(12);
int T=1;
// cin>>T;
while(T--)
{
O_o();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3872kb
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: 36ms
memory: 3648kb
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 Yes No No No No No No No ...
result:
wrong answer 89th words differ - expected: 'No', found: 'Yes'