Description
"Nibelung is a tree of n regions of the n-1 even elevated road since, every time Load, you will be reborn in a particular area. If the rally point is the entire focus of the Nibelungen, that is, the tree the focus, then you can in the shortest time with friends nono escape. " "
Yes, give you a little convenient slightly, you can choose a viaduct cut off, and then connect the other two places, each with only Load a skill, but it must still constitute the entire tree structure. Save your point here, Load will naturally restore the original Nibelungen slightly. "
Entry
The first line of two integers n, m, n As stated meaning, m denotes the path Mingfei Load m times.
Next, n-1 lines of two integers x, y represents a node x, y exists between an edge.
Next m lines, each an integer p, represents the rebirth of the Load area.
Export
For each inquiry, output a line
if it can then output "YES", otherwise a "NO" (without the quotes note)
Sample input
5 3
1 2
1 3
1 4
1 5
1
2
3
Sample Output
YES
NO
NO
Description:
I put a Pascal code that abruptly changed the c ++ code that ......
has passed? ?
analysis:
Find the center of gravity of the tree , ask x points , remove any of its sides , and connect any two points , find it can become a tree of focus
can find the largest and the second largest sub-tree center of gravity , if i is the largest sub-tree on, it will be time x the big tree on the child even , otherwise it will be the largest sub-tree with the x , and then determine at this point whether the center of gravity.
C ++ CODE:
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
int jl[400000],s[400000],g[400000],a[400000],f[400000],x[400000],y[400000];
int n,m,dian,i,p,ji,maxx,max2,jis,ans;
int h[400000][100];
bool bz[400000],zhi[400000],bo;
void dg(int k)
{
int i,p;
if(k==12) k=k;
for(i=1;i<=g[k];i++)
{
p=h[k][i];
if(bz[p]==false)
{
bz[p]=true;
dg(p);
f[k]=f[k]+f[p];
}
}
f[k]++;
}
void dg2(int k)
{
int i,p,t,s;
s=n-f[k];
if(bo)
return;
s--;
for(i=1;i<=g[k];i++)
{
p=h[k][i];
if(bz[p])
continue;
if(s<f[p])
s=f[p];
}
if(s<=n/2)
{
bo=true;
dian=k;
return;
}else
{
for(i=1;i<=g[k];i++)
{
if(bz[h[k][i]])
continue;
bz[h[k][i]]=true;
dg2(h[k][i]);
if(bo)
return;
}
}
}
void jilu(int k)
{
int i,p;
zhi[k]=true;
for(i=1;i<=g[k];i++)
{
p=h[k][i];
if(bz[p]==false)
{
bz[p]=true;
jilu(p);
}
}
}
int main(){
freopen("h3.in","r",stdin);
freopen("h3.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
g[x[i]]++;
h[x[i]][g[x[i]]]=y[i];
g[y[i]]++;
h[y[i]][g[y[i]]]=x[i];
}
for(int i=1;i<=m;i++)
{
scanf("%d",&a[i]);
}
memset(bz,false,sizeof(bz));
memset(f,0,sizeof(f));
bz[x[1]]=true;
dg(x[1]);
memset(bz,false,sizeof(bz));
bo=false;
bz[x[i]]=true;
dg2(x[1]);
memset(f,0,sizeof(f));
memset(bz,false,sizeof(bz));
bz[dian]=true;
dg(dian);
for(i=1;i<=g[dian];i++)
{
p=h[dian][i];
if(maxx<f[p])
{
maxx=f[p];
ji=p;
jis=i;
}
}
memset(bz,false,sizeof(bz));
memset(zhi,false,sizeof(zhi));
bz[dian]=true;
bz[ji]=true;
ans=1;
jilu(ji);
for(i=1;i<=g[dian];i++)
if(i!=jis)
{
p=h[dian][i];
if(max2<f[p])
max2=f[p];
}
for(i=1;i<=m;i++)
{
if(a[i]==dian)
{
cout<<"YES"<<endl;
continue;
}if(zhi[a[i]])
{
if(n-f[a[i]]-max2<=n/2)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else{
if(n-f[a[i]]-maxx<=n/2)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}