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 21 21 31 43 53 63 72 7

output
23

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

代码

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

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

  1. Codeforces Round #740 (Div. 2) F. Top-Notch Insertions 线段树 / 平衡树 + 组合数学

    传送门 文章目录 题意: 思路: 题意: 思路: 考虑最终的序列是什么鸭子的,首先序列肯定单调不降,也就是a1≤a2≤a3≤...≤ana_1\le a_2\le a_3\le ...\le a_na ...

  2. Codeforces Round #727 (Div. 2) F. Strange Array 线段树 + 区间合并 + 排序优化

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的数组,对每个位置iii求一个最大价值,价值计算方式如下:选择一个包含iii的[l,r][l,r][l,r],让后将其拿出来排序,之后价值 ...

  3. Codeforces Round #723 (Div. 2) D. Kill Anton 线段树 + 暴力

    传送门 文章目录 题意: 思路: 题意: 给你一个只有ANTOANTOANTO四个字母的字符串,你每次可以交换相邻两个,花费为111,让后让你打乱字符串,使得将打乱的字符串还原为原来的字符串的花费最小 ...

  4. Codeforces Round #619 (Div. 2) F. Super Jaber 多源bfs + 思维转换

    传送门 文章目录 题意: 思路: 题意: 给你一个矩阵,每个格子都有一个颜色kkk,每秒可以移动到相邻矩阵或者瞬移到同一颜色的任意矩阵.有qqq个询问,每次询问给出两个点,求从一个点到另一个点的最短时 ...

  5. Codeforces Round #328 (Div. 2) 592 B. The Monster and the Squirrel

    题意:Ari 每天起床的第一件事就是喂松鼠-然后他在他家的地板上画了一个正n边形(真闲-. 然后顺时针标号从1~n 然后从1开始向除了1以外的所有顶点连边 然后从2开始向除了2以外的所有顶点连边,但是 ...

  6. Codeforces Round #782 (Div. 2)-D. Reverse Sort Sum(树状数组)

    题目链接:Problem - D - Codeforces 题意:给定一个 01 序列 ,对  区间分别进行递增排序,得到  个序列,现给定  序列, ,即所有序列第  位的和.求给定 序列所对应的原 ...

  7. Codeforces Round #654 (Div. 2) F.Raging Thunder(线段树/区间合并)

    题目 一个长为n(n<=5e5)的仅由'>"和'<'构成的字符串, 代表球桌上在1到n的位置,有n个转换器, 此外,球桌上还有0到n,共n+1个球洞,假设此时球碰到i位置的 ...

  8. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  9. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  10. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

最新文章

  1. IntelliJ IDEA 2020.1 EAP2 发布:新增禅模式和 LightEdit 模式
  2. Rust-Cargo(3)
  3. Mysql数据库(七)——mysql高阶语句(上)
  4. ROS在编译生成自定义消息时报错ModuleNotFoundError: No module named 'em'
  5. 移动端js事件-了解
  6. 解析对象体内与方法体内引用内部方法的不同
  7. Java使用Swing实现五子棋。
  8. 江湖传说——阿里巴巴土话102句完整版
  9. Rabbitmq取消预取机制配置,配置手动确认后仍然java.lang.IllegalStateException: Channel closed; cannot ack/nack的问题
  10. 用Maple求偏导数
  11. java web 播放flv,实现网页中播放FLV文件的源代码
  12. 小程序上传身份证正反照,竖排照片横向展示
  13. 2016网易春季校园招聘产品策划笔试
  14. Android Room框架使用
  15. vue根据url获取内容axios_使用Vue.js和Axios从第三方API获取数据 — SitePoint
  16. os.path.dirname用法
  17. 手把手教你使用PHP生成推广海报
  18. CSS实用技巧第一讲:文字处理
  19. 屏幕分辨率及衬线字体
  20. CF794E Choosing Carrot

热门文章

  1. 零知识证明在匿名投票中的应用
  2. Kubernetes 小白学习笔记(27)--kubernetes的运维-Trouble Shooting方法
  3. 微信模板消息400001:invalid credential, access_token is invalid or not latest
  4. android java文件_android 项目下文件的作用
  5. linq判断集合中相同元素个数_使用Linq获取集合的最后N个元素?
  6. 2020-07-17
  7. BIO、NIO和AIO的区别简述
  8. 数据库读写分离这个坑,你应该踩过吧?
  9. Logstash5.X 日志搜集处理框架 安装配置
  10. 阶段3 2.Spring_03.Spring的 IOC 和 DI_12 注入集合数据