Codeforces Round #328 (Div. 2)D. Super M 虚树直径

D. Super M

Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of n cities, connected by n - 1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go from any city to any other city using only the given roads. There are m cities being attacked by humans. So Ari... we meant Super M have to immediately go to each of the cities being attacked to scare those bad humans. Super M can pass from one city to another only using the given roads. Moreover, passing through one road takes her exactly one kron - the time unit used in Byteforces.

However, Super M is not on Byteforces now - she is attending a training camp located in a nearby country Codeforces. Fortunately, there is a special device in Codeforces that allows her to instantly teleport from Codeforces to any city of Byteforces. The way back is too long, so for the purpose of this problem teleportation is used exactly once.

You are to help Super M, by calculating the city in which she should teleport at the beginning in order to end her job in the minimum time (measured in krons). Also, provide her with this time so she can plan her way back to Codeforces.

Input

The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 123456) - the number of cities in Byteforces, and the number of cities being attacked respectively.

Then follow n - 1 lines, describing the road system. Each line contains two city numbers ui and vi (1 ≤ ui, vi ≤ n) - the ends of the roadi.

The last line contains m distinct integers - numbers of cities being attacked. These numbers are given in no particular order.

Output

First print the number of the city Super M should teleport to. If there are many possible optimal answers, print the one with the lowest city number.

Then print the minimum possible time needed to scare all humans in cities being attacked, measured in Krons.

Note that the correct answer is always unique.

Sample test(s)
input
7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7
output
2
3
 
Note

In the first sample, there are two possibilities to finish the Super M's job in 3 krons. They are:

 and .

However, you should choose the first one as it starts in the city with the lower number.

 题意:给一棵树,其中有些点遭到了攻击,问从哪一点开始可以用最短的时间访问所有被攻击的点,并输出最少需要多少单位时间.

题解: 我们先构造出一颗包含所以被攻击点的虚树,再求其直径

         答案那么就是 (2−直径  )。

///1085422276

#include<bits/stdc++.h>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';ch=getchar();
    }return x*f;
}
//****************************************
const int  N=123456+50;
#define mod 1000000007
#define inf 1000000007

vector<int >G[N*2];
int Mark[N*2],d[N*2],q[N*2];
void dfs(int x,int pre) {
     int bo=Mark[x];
     for(int i=0;i<G[x].size();i++) {
        if(G[x][i]==pre) continue;
        d[G[x][i]]=d[x]+1;
        dfs(G[x][i],x);
        if(q[G[x][i]]) bo=1;
     }
     q[x]=bo;
}
int main() {
    int n,m,v,u,x,mx=0;
   n=read(),m=read();
   for(int i=1;i<=n-1;i++) {
    scanf("%d%d",&u,&v);
    G[u].pb(v);G[v].pb(u);
   }mem(Mark);
   for(int i=1;i<=m;i++) {
    scanf("%d",&x);Mark[x]=1;
   }
   mem(d);mem(q);
   dfs(1,-1);mx=0,v=1;
   for(int i=1;i<=n;i++) {
    if(Mark[i]&&d[i]>mx) {
        mx=d[i];v=i;
    }
   }mem(d);mx=0;
   int sum=0;
   dfs(v,-1);
   for(int i=1;i<=n;i++) {
    if(q[i]) {
        mx=max(mx,d[i]);sum++;
    }
   }
   for(int i=1;i<=n;i++) {
    if(q[i]&&d[i]==mx) {
        v=min(v,i);
    }
   }
   printf("%d\n%d\n",v,(sum-1)*2-mx);
  return 0;
}
代码

 

posted @ 2015-11-10 15:54  meekyan  阅读(208)  评论(0编辑  收藏  举报