题意:

给你一棵树,树上有些点是有人的,问你选一个点,最短的(最远的那个人的距离)是多少。

思路:

其实就是树的直径,两遍dfs,dfs第二遍的时候遇到人就更新直径就行了,ans是/2,奇数的话+1。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e5+; vector<vector<int> >v(N);
bool is[N],vis[N];
int pre1,pre2;
int max_=-; void dfs1(int u,int depth)
{
if(is[u])
{
if(max_<depth)
pre2=u,max_=depth;
}
vis[u]=;
int sz=v[u].size();
fo(i,,sz-)
{
int to=v[u][i];
if(!vis[to])
dfs1(to,depth+);
}
}
void dfs2(int u,int depth)
{
vis[u]=;
if(is[u])
max_=maxx(max_,depth);
int sz=v[u].size();
fo(i,,sz-)
{
int to=v[u][i];
if(!vis[to])
dfs1(to,depth+);
}
} int main()
{
int n,k;
sc("%d%d",&n,&k);
fo(i,,n-)
{
int a,b;
sc("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
fo(i,,k)
{
int a;
sc("%d",&a);
is[a]=;
pre1=a;
}
dfs1(pre1,);
mem(vis,);
max_=-;
dfs2(pre2,);
pr("%d\n",(max_+)/);
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}

最短meeting路线(树的直径)--牛客第四场(meeting)的更多相关文章

  1. 笛卡尔树--牛客第四场(sequence)

    思路: O(n)建一颗笛卡尔树,再O(n)dfs向上合并答案就行了. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include &l ...

  2. 线性基求交(线段树)--牛客第四场(xor)

    题意: 给你n个基,q个询问,每个询问问你能不能 l~r 的所有基都能表示 x . 思路: 建一颗线性基的线段树,up就是求交的过程,按照线段树区间查询的方法进行check就可以了. #define ...

  3. 分层最短路(牛客第四场)-- free

    题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...

  4. %300为0的个数(牛客第四场)-- number

    题意: 给你一串数,问你如题. 思路: 我不是这样的作法,从后往前,先取00,再算%3==0的个数,往前推的时候有递推关系: #define IOS ios_base::sync_with_stdio ...

  5. 3的倍数 或运算构造x(牛客第四场)-- triples I

    题意: 给你一个数,希望你能用最少的3的倍数或运算成它,让你输出答案. 思路: 进制%3有规律,1.2.4.8.16%3是1.2.1.2.1 ... 利用这一点分情况取一些位合成一些数就是答案了. # ...

  6. 树的子结构 牛客网 剑指Offer

    树的子结构 牛客网 剑指Offer 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) # class TreeNode: # def __init_ ...

  7. 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树

    目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...

  8. 线段树 B数据结构 牛客练习赛28

    链接:https://ac.nowcoder.com/acm/contest/200/B来源:牛客网 题目描述 qn姐姐最好了~     qn姐姐给你了一个长度为n的序列还有m次操作让你玩,     ...

  9. uestc summer training #9 牛客第三场 BFS计数

    G.coloring tree BFS计数 题目:给你n(<=5000)个节点的一颗树 你有K(<=5000)种颜色 你可以给每一个节点染一种颜色 总共有Kn种染色方法 在一种染色方法中 ...

随机推荐

  1. 推荐系统系列(五):Deep Crossing理论与实践

    背景 特征工程是绕不开的话题,巧妙的特征组合也许能够为模型带来质的提升.但同时,特征工程耗费的资源也是相当可观的,对于后期模型特征的维护.模型线上部署不太友好.2016年,微软提出Deep Cross ...

  2. python-matplotlib-ERROR

    在导入包显示画图时,出错:ImportError: No module named '_tkinter 解决办法:通常原因是tkinter和tk-devel缺失 ERROR: matplotlib的图 ...

  3. IE 不兼容 console 关键字

    如果在JS文件中写了console.log()方法,在IE下打开 开发者工具就没问题,不打开就有问题js 报错 不执行等等 ...... (IE这都是什么鬼!!),百度过后的解决方法如下: 好吧,这其 ...

  4. C++模板的偏特化与全特化

    模板的声明 类模板和函数模板的声明方式是一样的,在类定义/模板定义之前声明模板参数列表.例如: // 类模板 template <typename T1, typename T2> cla ...

  5. gcd表(欧几里得定理)

    题目:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=797 gcd表 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 ...

  6. Linux停止被占用的端口

    查找被占用的端口:netstat -lnp|grep 80 查看80端口被那些服务占用. kill掉该进程 kill -9 5574 然后在去查看一下80被占用的情况netstat -lnp|grep ...

  7. 基于 XML 的 AOP 配置(1)

    本文连接:https://www.cnblogs.com/qzhc/p/11969734.html 接下来我将用一个很简单的实例 1. 环境搭建 1.1. 第一步:准备必要的代码 业务层代码: Acc ...

  8. SpringSecurity remember-me功能

    1./login .and().formLogin().loginPage("/user/login.html") //在successHandler中,使用response返回登 ...

  9. MapInfo 文件解析

    在MapInfo 中所指的表是单纯的数据表或是图形与数据的结合.一个典型的MapInfo表将主要由*.tab.*.dat.*.wks.*.dbf. *.xls.*.map.*.id.*.ind文件格式 ...

  10. win10 c++程序打包

    步骤如下: 1. 先动态编译连链接,生成exe: 2. 找到exe依赖的dll文件 使用Process Explore来获取所依赖的dll文件 打开procexp.exe,通过菜单View–Lower ...