Codeforces Round #328 (Div. 2) D. Super M 虚树直径
D. Super M
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/592/problem/D
Description
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 road i.
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 Input
1 2
1 3
1 4
3 5
3 6
3 7
2 7
Sample Output
2
3
HINT
题意
给你一棵树,树上有一些重要的点,超人必须要去。
然后让你选择一个点作为起点,使得从这个点开始遍历其他的重要的点要走的距离最小
如果有多个解,就输出最小的那个
题解:
相当于建一颗包含所有重要的点,但是大小最小的虚树,然后再那个虚树里面找到字典序最小的直径
然后答案就是2*边长-直径长度就好了
代码
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cstring>
using namespace std;
#define maxn 123459
vector<int>Q[maxn];
int vis[maxn];
int vis2[maxn];
int ans1,ans2,len;
void dfs(int x,int dis,int pre)
{
if(dis>len&&vis[x])
{
len = dis;
ans2 = x;
}
if(dis==len&&vis[x]&&x<ans2)
ans2 = x;
for(int i=;i<Q[x].size();i++)
{
if(Q[x][i]==pre)
continue;
dfs(Q[x][i],dis+,x);
}
}
int ppp = ;
int dfs2(int x,int pre)
{
if(vis[x])vis2[x]++;
for(int i=;i<Q[x].size();i++)
{
if(Q[x][i]==pre)
continue;
vis2[x]+=dfs2(Q[x][i],x);
}
return vis2[x];
}
void dfs3(int x,int pre)
{
if(vis2[x])
ppp++;
for(int i=;i<Q[x].size();i++)
{
if(Q[x][i]==pre)
continue;
dfs3(Q[x][i],x);
}
}
int main()
{
int n,m;scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
Q[x].push_back(y);
Q[y].push_back(x);
}
ans1 = ;
for(int i=;i<=m;i++)
{
int x;scanf("%d",&x);
vis[x]=;
ans1 = min(x,ans1);
}
if(m==)
{
printf("%d\n0\n",ans1);
return ;
}
len = -;
dfs(ans1,,-);
ans1 = ans2;
len = -;
dfs(ans1,,-);
dfs2(ans2,-);
dfs3(ans2,-);
printf("%d\n",min(ans1,ans2));
if(ppp!=)ppp--;
printf("%d\n",ppp* - len);
}
Codeforces Round #328 (Div. 2) D. Super M 虚树直径的更多相关文章
- Codeforces Round #328 (Div. 2) D. Super M
题目链接: http://codeforces.com/contest/592/problem/D 题意: 给你一颗树,树上有一些必须访问的节点,你可以任选一个起点,依次访问所有的必须访问的节点,使总 ...
- Codeforces Round #328 (Div. 2)
这场CF,准备充足,回寝室洗了澡,睡了一觉,可结果... 水 A - PawnChess 第一次忘记判断相等时A先走算A赢,hack掉.后来才知道自己的代码写错了(摔 for (int i=1; ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #328 (Div. 2) C. The Big Race 数学.lcm
C. The Big Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/probl ...
- Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学
B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...
- Codeforces Round #328 (Div. 2) A. PawnChess 暴力
A. PawnChess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/ ...
- Codeforces Round #328 (Div. 2)_B. The Monster and the Squirrel
B. The Monster and the Squirrel time limit per test 1 second memory limit per test 256 megabytes inp ...
- Codeforces Round #328 (Div. 2)_A. PawnChess
A. PawnChess time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #328 (Div. 2) A
A. PawnChess time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
随机推荐
- Oracle 跟踪事件 set event
一.Oracle跟踪文件 Oracle跟踪文件分为三种类型,一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空间创建.回滚段创建.某些alter命令.日志切换.错误消息 ...
- 是时候学习Android分屏开发了
今年Google发布了Android N,Android N新增了不少功能,最受关注的自然就是分屏了. 这一功能对国内的很多手机用户并不陌生,其实很多第三方系统早已经实现了这一功能,如EMUI,Fly ...
- Log4NET简介
log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具. 前提 最近做项目需要记录系统日志和用 ...
- xml-xml试题
ylbtech-doc:xml-xml试题 xml试题 1.A,xml试题返回顶部 01.{XML题目}关于XML声明正确的是.(选择1项) A)<!xml version=”1.0”!> ...
- 《Python CookBook2》 第一章 文本 - 改变多行文本字符串的缩进 && 扩展和压缩制表符(此节内容待定)
改变多行文本字符串的缩进 任务: 有个包含多行文本的字符串,需要创建该字符串的一个拷贝.并在每行行首添加或者删除一些空格,以保证每行的缩进都是指定数目的空格数. 解决方案: # -*- coding: ...
- C++ 编程输入输出语句
C++ 的标准输入.输出就是我们已经使用的包含头文件iostream,他不但提供了I/O的库函数,也提供了使用该库的流模式,从cin>> 流入 和cout<<流出到设备就是一 ...
- mybatis系列-05-SqlMapConfig.xml详解
mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases(类型别名) typeHandler ...
- 重新起步 iOS 开发
25Dec2013 Stanford iOS 公开课看到第三课 Programing in Objective-C 2.0 看完了第一部分,基本是以半小时一章的速度浏览的
- PHP命名规范【转】
[转]谭博的个人网站 [类] 1.类名与类文件名采用驼峰式且首字母大写 2.类私有属性和私有方法名称以下划线开头 3.方法名使用驼峰式 [变量] 变量名使用小写字母加下划线 [函数] 函数名使用小 ...
- Bmob第三方登录详解
Bmob第三方登录详解 Bmob 第三方登录 简介 本文主要介绍新浪微博,QQ,微信的登录接入以及如何配合BmobSDK中的第三方登录功能实现第三方登录. 在使用之前请先按照快速入门创建好可以调用Bm ...