链接: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,把求出的最小负距离取反就得到最大正距离

代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=2e5+5,inf=0x3f3f3f3f;
 4 int n,k,dis[amn];
 5 int p[amn];
 6 struct node{
 7     int pos,val;
 8     bool operator <(const node &a)const {return a.val<val;}
 9 };
10 struct edge{
11     int to,w,next;
12 }e[amn];
13 priority_queue<node> que;
14 int head[amn],edgenum=0,vis[amn];
15 void add(int f,int t,int co)
16     {
17         e[++edgenum].next=head[f];
18         head[f]=edgenum;
19         e[edgenum].to=t;
20         e[edgenum].w=co;
21     }
22 void Dijkstra(int s)
23     {
24         memset(dis,0x3f,sizeof(dis));
25         memset(vis,0,sizeof vis);
26         while(que.size())que.pop();
27         dis[s]=0;
28         node a;
29         a.pos=s,a.val=dis[s];
30         que.push(a);
31         while(!que.empty())
32             {
33                 node x=que.top();que.pop();
34                 int u = x.pos;
35                 if(x.val > dis[u]) continue;
36                 vis[u]=true;
37                 for(int i=head[u];i!=-1;i=e[i].next)
38                     {
39                         int v=e[i].to;
40                         if(vis[v])    continue;
41                         if(dis[v]>e[i].w+dis[u])
42                             {
43                                 dis[v]=e[i].w + dis[u];
44                                 a.pos=v,a.val=dis[v];
45                                 que.push(a);
46                             }
47                     }
48             }
49     }
50 int main(){
51     memset(head,-1,sizeof head);
52     ios::sync_with_stdio(0);
53     cin>>n>>k;
54     int u,v,ans;
55     for(int i=1;i<n;i++){
56         cin>>u>>v;
57         add(u,v,-1);
58         add(v,u,-1);
59     }
60     for(int i=1;i<=k;i++){
61         cin>>p[i];
62     }
63     Dijkstra(p[1]);
64     int maxn=inf,maxi=0;
65     for(int i=1;i<=k;i++){
66         if(dis[p[i]]<maxn){
67             maxn=dis[p[i]];
68             maxi=i;
69         }
70     }
71     Dijkstra(p[maxi]);
72     ans=inf;
73     for(int i=1;i<=k;i++){
74         if(dis[p[i]]<ans){
75             ans=dis[p[i]];
76         }
77     }
78     ans=-ans;
79     printf("%d\n",ans/2+ans%2);
80 }

 

转载于:https://www.cnblogs.com/brainm/p/11257119.html

2019牛客多校第四场 A meeting相关推荐

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

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

  2. 2019牛客多校第四场 B xor (线性基求交)

    xor 思路 题目是要求[l,r][l, r][l,r]的所有集合是否都可以得到xxx,那么显然我们可以对这[l,r][l, r][l,r]个线性基求交,然后再特判能否xxx能否插入,如果能插入,显然 ...

  3. 牛客多校第四场【B-Basic Gcd Problem】

    牛客多校第四场[B-Basic Gcd Problem] 题目链接:https://ac.nowcoder.com/acm/contest/5669/B 思路:先要理解公式,多看几个数据基本就会有点想 ...

  4. 2019牛客多校训练营第一场 H题 HOR 题解

    题目描述: 输入描述: 输出描述: 示例1: 题解: 更多问题可关注牛客竞赛区,一个刷题.比赛.分享的社区. 传送门:https://ac.nowcoder.com/acm/contest/discu ...

  5. 2019牛客多校 第七场 B Irreducible Polynomial 多项式因式分解判断

    链接:https://ac.nowcoder.com/acm/contest/887/B 来源:牛客网 Irreducible Polynomial 时间限制:C/C++ 1秒,其他语言2秒 空间限制 ...

  6. 2019牛客多校训练营第一场 E题 ABBA 题解

    问题描述: 输入描述: 输出描述: 示例1: 题解: 更多问题可关注牛客竞赛区,一个刷题.比赛.分享的社区. 传送门:https://ac.nowcoder.com/acm/contest/discu ...

  7. 2019牛客多校第七场 C Governing sand

    因为当时时间不怎么够就没写... 其实就是一个模拟题而已下面注释很清楚 链接:https://ac.nowcoder.com/acm/contest/887/C 来源:牛客网 时间限制:C/C++ 3 ...

  8. [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)

    题目链接:https://ac.nowcoder.com/acm/contest/886/B 题意: 您将获得一个IPv6地址,该地址是128位二进制字符串.请根据以下规则确定其最短的表示: 以十六进 ...

  9. 2022 年牛客多校第四场补题记录

    A Task Computing 题意:给定长度为 nnn 的序列 {(wi,pi)}\{(w_i,p_i)\}{(wi​,pi​)},从中选出 mmm 项并重新排列得到子序列 {a1,a2,⋯,am ...

  10. 2019牛客多校第六场 E Androgynos

    传送门:https://ac.nowcoder.com/acm/contest/886/E 首先要同构的话,必须补图和原图边数一样,完全图总边数就必须是偶数,那么只有n%4=1 和 n%4=0时总边数 ...

最新文章

  1. Smart Card知识
  2. 不能摸鱼的工作不是好工作?
  3. 更新电脑游戏安装合集,600+单机游戏,单机电脑游戏合集共约4T
  4. Oracle使用遇到的问题
  5. 【NLP】PET——文本分类的又一种妙解
  6. Scala Iterator(迭代器)详解
  7. jQuery EasyUI window窗口使用实例
  8. Marble原理之线程中断
  9. 无线数传在桥梁检测中传感器信号的采集应用
  10. android 按钮带图标 阴影_android中带图标的按钮(ImageButton)怎么用
  11. 怎样在ppt中加入随机抽号_潮流女生怎样穿更时髦?经典中加入个性,减龄时尚还高级,快入坑...
  12. 米家小白增强固件_中考体育:男1000米/女800米想拿满分,掌握呼吸法是关键
  13. 使用了无效的sql语句_使用SQL语句创建数据库
  14. 从零开始学习音视频编程技术--转自雲天之巔
  15. sqlite3 not found问题解决方法
  16. Silverlight 2中多语言支持实现(下)
  17. 网络存储技术 (HUAWEI)
  18. Rhadoop的安装
  19. java javaw 命令区别_java、javaw和javaws的区别
  20. 奥克兰大学商学院计算机专业,奥克兰大学的商科专业 推荐三大专业

热门文章

  1. 二分法其实很简单,为什么老是写不对!!!
  2. Wattagio for Mac(电池管理) 免激活版
  3. 苹果MAC全能多媒体播放器:OmniPlayer Pro
  4. Macs Fan Control Pro for mac(电脑风扇控制软件)v1.5.10中文
  5. Mac效率工具:Mosaic 1.3.3
  6. 01数据结构——绪论
  7. 报告显示:数据屏蔽降低业务安全风险
  8. Android学习笔记(18):编辑框EditText
  9. 金蝶BOS开发之--非空验证、时间、电话号码验证
  10. arraylist linkedlist vector 三者区别