j题意·:给你n个点,并给出连接关系和每个点的颜色,问是否能找到一点,使它的每个子树颜色相同,可以用DFS来做,也有简单的方法,因为每个子树的颜色都相同,在那一点和子树连接的是不同的颜色,所以记录每个连接不同颜色的点。
#include<cstdio>
#include<cstring>
#include<algorithm>
const int N=10010;
using namespace std;
int v[N],u[N],c[N],cnt[N];
int main()
{
int n;
int sum=0;
memset(cnt,0,sizeof(cnt));
scanf("%d",&n);
for(int i=1;i<n;i++)
{
scanf("%d%d",&u[i],&v[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&c[i]);
}
for(int i=1;i<n;i++)
{
if(c[u[i]]!=c[v[i]])
{
sum++;
cnt[u[i]]++;
cnt[v[i]]++;
}
}
int flag=0;
for(int i=1;i<=n;i++)
{
if(sum==cnt[i])
{
flag=1;
printf("YES\n%d\n",i);
break;
}
}
if(flag==0)
{
printf("NO\n");
}
return 0;
}