Equidistant Gym - 102411E

╄→гoц情女王★ 提交于 2019-11-27 03:30:36

In 2019 ICPC subregions structure was changed a little. Now for each subregion, we need to choose the best place for the subregion finals. To make things fair we want to choose a city in such a way that all teams will spend the same amount of time to get there.

To make things simpler we say that all teams will use trains to get to the finals. The railroad system can be represented as a tree where each city is a vertex and some pairs of cities are connected by a railroad. It takes exactly one hour to get from one city to another if they are directly connected.

You are given a description of the railroad system and the cities where teams are located. You need to choose any city that has an equal distance to all teams' cities or detect that no such city exists.

Input

The first line of the input contains two integers nn and mm — the number of cities and the number of teams (1≤m≤n≤2⋅1051≤m≤n≤2⋅105). Each of the following n−1n−1 lines contains two integers vivi and uiui — the indices of the cities connected by the ii-th railroad (1≤vi,ui≤n1≤vi,ui≤n). It is guaranteed that for each pair of cities there is exactly one simple path connecting them.

The next line contains mm integers c1,c2,…,cmc1,c2,…,cm — the cities of the teams (1≤ci≤n1≤ci≤n). All teams are located in different cities.

Output

If it is impossible to choose a city fairly, output a single word "NO". Otherwise, output a the word "YES" at the first line. The second line should contain a single integer — the city where subregion finals should be held. If there is more than one solution, output any of them.

Examples

Input

6 3
1 2
2 3
3 4
4 5
4 6
1 5 6

Output

YES
3

Input

2 2
1 2
1 2

Output

NO

给定n个点n-1条边的树,求到m个点的距离相等的点

把m个点放进队列,每次bfs一层,找被bfs相同层数的m次的点

 

#include<bits/stdc++.h>
using namespace std;
const int maxn =  2e5+10;
struct node
{
    int to,next;
} edge[2*maxn];
int head[maxn],cnt,vis[maxn],deep[maxn],num[maxn],m,n;
void add(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
queue<int>q;
void bfs()
{
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        for(int i = head[x]; i>=0; i = edge[i].next)
        {
            int to = edge[i].to;
            if(deep[to]==0||deep[to] == deep[x]+1)
            {
                num[to]+=num[x];
                deep[to] = deep[x]+1;
                if(num[to] == m)
                {
                    printf("YES\n%d\n",to);
                    exit(0);
                }
                if(!vis[to])
                    q.push(to);
                    vis[to] = 1;
            }
        }
    }
}
int main()
{
    int i,j,a,b;
    scanf("%d %d",&n,&m);
    memset(head,-1,sizeof(head));
    cnt = 0;
    for(i = 0; i<n-1; i++)
    {
        scanf("%d %d",&a, &b);
        add(a,b);
        add(b,a);
    }
    for(i = 0; i < m; i++)
    {
        scanf("%d",&a);
        q.push(a);
        deep[a] = 1;
        vis[a] = 1;
        num[a] = 1;
    }
    if(m == 1)
    {
        printf("YES\n%d\n",a);
        return 0;
    }
    bfs();
    printf("NO\n");



    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!