HTML & JS
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define pb push_back
#define powerof2 ()
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1= 998244353;
using vl = vector<ll>;
using pll =  pair<ll,ll>;
typedef tree<ll,null_type,less<ll>,rb_tree_tag, tree_order_statistics_node_update> is;


int n;
vector<vector<int>> tre;
vector<int> entry,parent,leaves;
void dfs(int c,int p,int va){
	entry[c]=va;
	parent[c]=p;
	leaves[c]=0;
	if(tre[c].size()==1)
		leaves[c]=1;
	for(int i=0;i<tre[c].size();i++)
	{
		if(tre[c][i]!=p)
		{
			dfs(tre[c][i],c,entry[c]+leaves[c]);
			leaves[c]+=leaves[tre[c][i]];
		}
	}
}
void solve(){
	int m;
	cin>>n>>m;
	int u,v;
	tre.resize(n+1);
	entry.resize(n+1);
	leaves.resize(n+1);
	parent.resize(n+1);
	for(int i=0;i<n-1;i++)
	{
		cin>>u>>v;
		tre[u].pb(v);
		tre[v].pb(u);
	}
	tre[1].pb(0);
	dfs(1,0,0);
	parent[1]=1;
	while(m--)
	{
		int k;
		cin>>k;
		vector<int> span(k);
		int mi=(int)1e9;
		for(int i=0;i<k;i++)
		{
			cin>>span[i];
			span[i]=parent[span[i]];
			if(entry[span[i]]<mi)
			{
				v=span[i];
				mi=entry[v];
			}
		}
		int c=0,va=entry[v]+leaves[v];
		for(int i=0;i<k;i++)
		{
			if(entry[span[i]]+1<=va)
				c++;
		}
		if(c==k)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
}
int main(){

	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    solve();
    return 0;
}

Generated URL:

Output:

YES
YES
YES
YES
NO
NO
Report Bug