链接: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%);
}

 

2019牛客多校第四场 A meeting的更多相关文章

  1. 2019牛客多校第四场A meeting——树的直径

    题意: 一颗 $n$ 个节点的树上标有 $k$ 个点,找一点使得到 $k$ 个关键结点的最大距离最小. 分析: 问题等价于求树的直径,最小距离即为直径除2向上取整. 有两种求法,一是动态规划,对于每个 ...

  2. 2019牛客多校第四场A meeting 思维

    meeting 题意 一个树上有若干点上有人,找出一个集合点,使得所有人都到达这个点的时间最短(无碰撞) 思路 就是找树的直径,找直径的时候记得要找有人的点 #include<bits/stdc ...

  3. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  4. 2019牛客多校第四场B xor——线段树&&线性基的交

    题意 给你 $n$ 个集合,每个集合中包含一些整数.我们说一个集合表示一个整数当且仅当存在一个子集其异或和等于这个整数.现在你需要回答 $m$ 次询问 ($l, r, x$),是否 $l$ 到 $r$ ...

  5. 2019牛客多校第四场J free——分层图&&最短路

    题意 一张无向图,每条边有权值,可以选择不超过 $k$ 条路使其权值变成0,求 $S$ 到 $T$ 的最短路.(同洛谷 P4568) 分析 首先,分层图最短路可以有效解决这种带有 「阶段性」的最短路, ...

  6. [2019牛客多校第四场][G. Tree]

    题目链接:https://ac.nowcoder.com/acm/contest/884/G 题目大意:给定一个树\(A\),再给出\(t\)次询问,问\(A\)中有多少连通子图与树\(B_i\)同构 ...

  7. 2019牛客多校第四场D-triples I 贪心

    D-triples 题意 给你一个\(n\),问至少有几个数或运算起来可以等于\(n\),并且输出数量和这个几个数.题目说明给的\(n\)一定符合条件(不会输出\(n= 1\) 之类不存在情况). 思 ...

  8. 2019牛客多校第四场C-sequence(单调栈+线段树)

    sequence 题目传送门 解题思路 用单调栈求出每个a[i]作为最小值的最大范围.对于每个a[i],我们都要乘以一个以a[i]为区间内最小值的对应的b的区间和s,如果a[i] > 0,则s要 ...

  9. 2019牛客多校第四场K number dp or 思维

    number 题意 给一个数字串,问有几个子串是300的倍数 分析 dp写法:这题一看就很dp,直接一个状态dp[i][j]在第i位的时候膜300的余数是j左过去即可.这题比赛的时候样例老是少1,后面 ...

随机推荐

  1. X因素 开启它就能让你成为超级明星

    开启它就能让你成为超级明星" title="X因素 开启它就能让你成为超级明星"> "只要努力就能成为明星!"记得电影学院的不少老师都这样告诫学 ...

  2. JavaScript 設計模型 - Iterator

    Iterator Pattern是一個很重要也很簡單的Pattern:迭代器!我們可以提供一個統一入口的迭代器,Client只需要知道有哪些方法,或是有哪些Concrete Iterator,並不需要 ...

  3. The difference between applicationContext.xml in Spring and xxx-servlet.xml in SpringMVC

    一直搞不明白两者的区别.如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 因为 ...

  4. C++扬帆远航——16(猜数字)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:guessnum.cpp * 作者:常轩 * 微信公众号:Wor ...

  5. win10 pycharm调试技巧 Debug

    1.设置断点 2.调试方法对比 step into:单步执行,遇到子函数就进入并且继续单步执行(简而言之,进入子函数): step over:在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行 ...

  6. Golang 使用Protocol Buffer 案例

    目录 1. 前言 2. Protobuf 简介 2.1 Protobuf 优点 2.2 Protobuf 缺点 2.3 Protobuf Golang 安装使用 3. Protobuf 通讯案例 3. ...

  7. Flex实现九宫格

    写一个靠谱的flex布局 <!DOCTYPE html> <html> <style> .block { padding-top: 30%; margin-top: ...

  8. 微信小程序中图片上传阿里云Oss

    本人今年6月份毕业,最近刚在上海一家小公司实习,做微信小程序开发.最近工作遇到一个小问题. 微信小程序图片上传阿里云服务器Oss也折腾了蛮久才解决的,所以特意去记录一下. 第一步:配置阿里云地址: 我 ...

  9. HashSet底层、及存入对象时候如何保持唯一

    HashSet底层.及存入对象时候如何保持唯一 在JDK1.8之前,哈希表底层采用数组+链表实现,即使用链表处理冲突,同一hash值的链表都存储在一个链表里. 但是当位于一个桶中的元素较多,即hash ...

  10. MATLAB神经网络(2) BP神经网络的非线性系统建模——非线性函数拟合

    2.1 案例背景 在工程应用中经常会遇到一些复杂的非线性系统,这些系统状态方程复杂,难以用数学方法准确建模.在这种情况下,可以建立BP神经网络表达这些非线性系统.该方法把未知系统看成是一个黑箱,首先用 ...