博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #328 (Div. 2)D. Super M 虚树直径
阅读量:4489 次
发布时间:2019-06-08

本文共 3484 字,大约阅读时间需要 11 分钟。

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
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_backinline 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 1000000007vector
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
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;}
代码

 

转载于:https://www.cnblogs.com/zxhl/p/4953302.html

你可能感兴趣的文章
Webform(分页、组合查询)
查看>>
Foundation - NSDate
查看>>
Codeforces - 570D 离散DFS序 特殊的子树统计 (暴力出奇迹)
查看>>
geatpy - 遗传和进化算法相关算子的库函数(python)
查看>>
iOS 线程安全
查看>>
mysql 分组之后统计记录条数
查看>>
New STL Algorithms That Will Make A More Productive Developer
查看>>
js 对象 浅拷贝 和 深拷贝
查看>>
初识 python
查看>>
PCL Examples
查看>>
spring boot
查看>>
浏览器URL传参最大长度问题
查看>>
学习进度条
查看>>
Linux crontab 定时任务详解
查看>>
string成员函数
查看>>
onSaveInstanceState()方法问题
查看>>
[转]CocoaChina上一位工程师整理的开发经验(非常nice)
查看>>
大数据时代侦查机制有哪些改变
查看>>
L1-047 装睡
查看>>
雷林鹏分享:jQuery EasyUI 菜单与按钮 - 创建链接按钮
查看>>