链接: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. App自动化测试方案

    App自动化测试方案 1.1  概述 什么是App自动化?为什么要做App自动化? App自动化是指给 Android或iOS上的软件应用程序做的自动化测试. 手工测试和自动化测试的对比如下: 手工测 ...

  2. 使用mysql创建自己的物化视图

    物化视图,它是用于预先计算并保存表连接或聚集等耗时较多的操作的结果,这样,在执行查询时,就可以避免进行这些耗时的操作,从而快速的得到结果.物化视图有很多方面和索引很相似:使用物化视图的目的是为了提高查 ...

  3. USB小白学习之路(2)端点IN/OUT互换

    端点2(out)和端点6(in)的out_in互换 注:这里的out和in都是以host为标准说的,out是host的out,在设备(Cy7c68013)这里其实是输入端口:in是host的in,在设 ...

  4. 达拉草201771010105《面向对象程序设计(java)》第十三周学习总结

    达拉草201771010105<面向对象程序设计(java)>第十三周学习总结 第一部分:理论知识 事件处理基础: 事件源:能够产生事件的对象都可 以成为事件源,如文本框.按钮等.一个事件 ...

  5. cocoapods iOS类库管理工具的安装与使用

    CocoaPods是一个管理Swift和Objective-C的Cocoa项目的依赖工具.他可以优雅地帮助你扩展你的项目.简单的说,就是替你管理Swift和Objective-C的Cocoa项目的第三 ...

  6. 难道同事:Java 方法调用到底是传值还是传引用

    Java 方法调用中的参数是值传递还是引用传递呢?相信每个做开发的同学都碰到过传这个问题,不光是做 Java 的同学,用 C#.Python 开发的同学同样肯定遇到过这个问题,而且很有可能不止一次. ...

  7. RTMP协议推流交互流程

    目录 RTMP协议推流交互流程 RTMP协议推流流程 RTMP握手 RTMP建立连接 RTMP建流&Play Wireshark抓个RTMP流 RTMP协议推流交互流程 想了解下直播常见协议R ...

  8. Dubbo 入门-细说分布式与集群

    什么是Dubbo Dubbo是一款高性能.轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现. 什么是RPC RPC全称(Rem ...

  9. Subsequence POJ - 3061

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22040   Accepted: 9404 Desc ...

  10. Win32 按钮嵌套收不到消息解决记录

    太长不看 SetWindowSubClass,然后 return DefSubclassProc(hWnd, uMsg, wParam, lParam);,不要有 WS_CHILD 这个 Style. ...