链接: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. Laravel Study(使用 Laravel )

    開始 伺服器及相關工具安裝自行建立,在伺服器跟目錄下 有兩種方式建立 Laravel 專案,這裡使用 composer 建立專案 使用 composer 要在 PHP 5.3.2 以上才能使用 com ...

  2. Linux用户组的添加及属性的更改

    用户组的创建: 12345 groupadd [OPTION] 组名 -g GID 指明GID号:[GID_MIN, GID_MAX] -r 创建系统组 CentOS 6: ID<500 Cen ...

  3. springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据

    springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...

  4. dns原理介绍及实践问题总结

    1 问题引入: a) 域名劫持: dns过程中某个环节被攻击/篡改,导致dns结果为劫持者的服务器.例如竞争对手将你方的app下载地址篡改为他方的app下载地址. b) 对现网用户进行监控时,发现个别 ...

  5. JMeter-完成批量的接口测试

    前言 当我们在工作中进行接口测试时,项目的接口肯定不止一个,而是很多很多,而且每个接口都需要进行正确参数,错误参数,参数为空,特殊字符等方式来测试接口是否能够正确返回所需的响应值. 今天,我们来一起学 ...

  6. 7-2 jmu-python-九九乘法表(矩形) (10 分)

    本题目要求输出如下图所示的九九乘法表 注:乘积要求做格式控制,占4个位置的宽度 输入样例: 无 输出样例: 1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8= ...

  7. 我厌倦了 Redux,那就造个轮子 Rectx:第三集

    仓库:215566435/rectx 前言 麻烦快去我的仓库里面喷: 老子学不动了,求不要更新. 呵呵,你没想到吧,这玩意儿竟然有第三集!我靠,我自己都没想到,让我们悄悄的回顾一下前两集完全没想到,竟 ...

  8. web页面上展示图片时,图片不显示,报错:ERR_CONTENT_LENGTH_MISMATCH

      问题描述 前端页面加载css,和js文件的时候,经常出现ERR_CONTENT_LENGTH_MISMATCH的报错情况.   查找问题 在单独打开hearder中css,js的网络地址是能打开的 ...

  9. PYTHON 第二天学习记录

  10. java算法--普通队列

    数据结构队列 首先明确一下队列的概念. 队列是一种有序列表,使用数组的结构来存储队列的数据. 队列是一种先进先出的算法.由前端加入,由后端输出. 如下图: ​ 第一个图 第二个图 第三个图 这就是队列 ...