题意:

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

思路:

其实就是树的直径,两遍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. numpy中np.linalg.norm()求向量、矩阵的范数

    np.linalg.norm() # linalg = linear(线性) + algebra(代数),   norm表示范数 x_norm = np.linalg.norm(x, ord=None ...

  2. CentOS8编译openocd-0.10.0

    1.sudo yum install libusbx-devel 2. ./configure --prefix=$HOME/openocd-0.10.0 --enable-jlink CFLAGS= ...

  3. JavaWeb_(Hibernate框架)Hibernate中事务

    Hibernate中事务 事务的性质 事物的隔离级别 配置事务的隔离级别 事务的性质 原子性:原子,不可再分,一个操作不能分为更小的操作,要么全都执行,要么全不执行. 一致性:事务在完成时,必须使得所 ...

  4. lcx用法

    lcx使用方 本机IP:192.168.125.11      目标机IP:192.168.125.101 本机运行: lcx -listen 3333  2222 目标机运行:lcx -slave ...

  5. [题解] [AHOI2009] 跳棋

    题面 题解 分类讨论, 考虑到只要所有的偶数点上都有棋子, 最左边的棋子就可以跳到最右边 题目第一问让我们求最少的在白格子上必须放的棋子数(不用考虑行动中放的棋子数) 考虑到这几种情况 有不少于两个红 ...

  6. Data Science Competition中的工具汇总

    除了基础的pandas,scikit-learn,numpy,matplotlib,seaborn以外 ( 1 ) category_encoders github 属于scikit-learn co ...

  7. Linux下MySQL报Table 'xxx' doesn't exist错误解决方法,表名存在大小写区分

    Linux服务器上在线装了个MySQL,但是部署web应用时一直报后台一直报错:Table 'xxx' doesn't exist. 本地测试一直都是正常的,同样的代码,同样的数据库,表是存在的,但是 ...

  8. SpringBoot2.X 静态文件配置

    Spring Boot 默认会挨个从 META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回. 默认配置 ...

  9. EasyUI中对于Grid的隐藏与显示

    $('#div_Grid').datagrid('hideColumn', 'mtnDate'); $('#div_Grid').datagrid('showColumn', 'mtnDate');

  10. 图片上传(前端显示预览,后端php接收)

    html: <form action="{:Url('do_ls_law_upload')}" method="POST" accept-charset= ...