链接:https://ac.nowcoder.com/acm/contest/884/A
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

A new city has just been built. There're nnn interesting places numbered by positive numbers from 111 to nnn.
In order to save resources, only exactly n−1n-1n−1 roads are built to connect these nnn interesting places. Each road connects two places and it takes 1 second to travel between the endpoints of any road.
There is one person in each of the places numbered x1,x2…xkx_1,x_2 \ldots x_kx1​,x2​…xk​ and they've decided to meet at one place to have a meal. They wonder what's the minimal time needed for them to meet in such a place. (The time required is the maximum time for each person to get to that place.)

输入描述:

First line two positive integers, n,kn,kn,k - the number of places and persons.
For each the following n−1n-1n−1 lines, there're two integers a,ba,ba,b that stand for a road connecting place aaa and bbb. It's guaranteed that these roads connected all nnn places.
On the following line there're kkk different positive integers x1,x2…xkx_1,x_2 \ldots x_kx1​,x2​…xk​ separated by spaces. These are the numbers of places the persons are at.

输出描述:

A non-negative integer - the minimal time for persons to meet together.
示例1

输入

复制

4 2
1 2
3 1
3 4
2 4

输出

复制

2

说明

They can meet at place 1 or 3.
1<=n<=1e5 题意 给出一颗树和一些点,一个点到另一个点的距离为1,求这些点在树上最大距离的一半 思路
要求图上的最大距离可先任意取一个点求最大距离,再从最大距离的这个点再找一次最大距离(因为重新求的最大距离必定大于或等于原先的最大距离)
这里求最大距离用了边权取负再dijkstra,把求出的最小负距离取反就得到最大正距离 代码
 #include<bits/stdc++.h>
using namespace std;
const int amn=2e5+,inf=0x3f3f3f3f;
int n,k,dis[amn];
int p[amn];
struct node{
int pos,val;
bool operator <(const node &a)const {return a.val<val;}
};
struct edge{
int to,w,next;
}e[amn];
priority_queue<node> que;
int head[amn],edgenum=,vis[amn];
void add(int f,int t,int co)
{
e[++edgenum].next=head[f];
head[f]=edgenum;
e[edgenum].to=t;
e[edgenum].w=co;
}
void Dijkstra(int s)
{
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof vis);
while(que.size())que.pop();
dis[s]=;
node a;
a.pos=s,a.val=dis[s];
que.push(a);
while(!que.empty())
{
node x=que.top();que.pop();
int u = x.pos;
if(x.val > dis[u]) continue;
vis[u]=true;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].to;
if(vis[v]) continue;
if(dis[v]>e[i].w+dis[u])
{
dis[v]=e[i].w + dis[u];
a.pos=v,a.val=dis[v];
que.push(a);
}
}
}
}
int main(){
memset(head,-,sizeof head);
ios::sync_with_stdio();
cin>>n>>k;
int u,v,ans;
for(int i=;i<n;i++){
cin>>u>>v;
add(u,v,-);
add(v,u,-);
}
for(int i=;i<=k;i++){
cin>>p[i];
}
Dijkstra(p[]);
int maxn=inf,maxi=;
for(int i=;i<=k;i++){
if(dis[p[i]]<maxn){
maxn=dis[p[i]];
maxi=i;
}
}
Dijkstra(p[maxi]);
ans=inf;
for(int i=;i<=k;i++){
if(dis[p[i]]<ans){
ans=dis[p[i]];
}
}
ans=-ans;
printf("%d\n",ans/+ans%);
}

 

最新文章

  1. js实例--飞机大战
  2. 使用 Google Web Fonts
  3. 关于对于IT我自己的见解以及我踩过的坑(需要认真读文章才能理解我所遇到的坑.)
  4. CGI、FastCGI和PHP-FPM浅析
  5. DBAPI部署
  6. 布局转换:文档流-&gt;绝对定位
  7. HDU 2897 邂逅明下(巴什博奕)
  8. 使用gcc编译gdb调试
  9. Android的Spinner
  10. Android M Developer Preview - API Preview(一)
  11. How can I let the compiled script depend on something dynamic
  12. linux下一个C语言flock功能使用 .
  13. TI公司与MSP430单片机
  14. The account &#39;...&#39; is no team with ID &#39;...&#39;
  15. 从String类型字符串的比较到StringBuffer和StringBuilder
  16. 【TOMCAT启动异常】The BASEDIR environment variable is not defined correctly
  17. sudo解决方案企业级应用实战
  18. 使用idea对spring boot项目打jar和war包[文件]
  19. np.unravel_index
  20. BEM思想之彻底弄清BEM语法

热门文章

  1. Android apk签名详解&mdash;&mdash;AS签名、获取签名信息、系统签名、命令行签名
  2. 没有admin权限如何免安装使用Node和NPM
  3. JVM性能优化系列-(7) 深入了解性能优化
  4. js操作复选框
  5. flask-restful 初探
  6. 蚂蚁金服开源 | 可视化图形语法G2 3.3 琢磨
  7. 网站开发---js与java实现的一些小功能
  8. 谈谈集合.Queue
  9. docker 搭建本地私有仓库
  10. Python基础类型(1)