最短meeting路线(树的直径)--牛客第四场(meeting)
题意:
给你一棵树,树上有些点是有人的,问你选一个点,最短的(最远的那个人的距离)是多少。
思路:
其实就是树的直径,两遍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)的更多相关文章
- 笛卡尔树--牛客第四场(sequence)
思路: O(n)建一颗笛卡尔树,再O(n)dfs向上合并答案就行了. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include &l ...
- 线性基求交(线段树)--牛客第四场(xor)
题意: 给你n个基,q个询问,每个询问问你能不能 l~r 的所有基都能表示 x . 思路: 建一颗线性基的线段树,up就是求交的过程,按照线段树区间查询的方法进行check就可以了. #define ...
- 分层最短路(牛客第四场)-- free
题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...
- %300为0的个数(牛客第四场)-- number
题意: 给你一串数,问你如题. 思路: 我不是这样的作法,从后往前,先取00,再算%3==0的个数,往前推的时候有递推关系: #define IOS ios_base::sync_with_stdio ...
- 3的倍数 或运算构造x(牛客第四场)-- triples I
题意: 给你一个数,希望你能用最少的3的倍数或运算成它,让你输出答案. 思路: 进制%3有规律,1.2.4.8.16%3是1.2.1.2.1 ... 利用这一点分情况取一些位合成一些数就是答案了. # ...
- 树的子结构 牛客网 剑指Offer
树的子结构 牛客网 剑指Offer 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) # class TreeNode: # def __init_ ...
- 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树
目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...
- 线段树 B数据结构 牛客练习赛28
链接:https://ac.nowcoder.com/acm/contest/200/B来源:牛客网 题目描述 qn姐姐最好了~ qn姐姐给你了一个长度为n的序列还有m次操作让你玩, ...
- uestc summer training #9 牛客第三场 BFS计数
G.coloring tree BFS计数 题目:给你n(<=5000)个节点的一颗树 你有K(<=5000)种颜色 你可以给每一个节点染一种颜色 总共有Kn种染色方法 在一种染色方法中 ...
随机推荐
- Mysql模拟故障恢复案例过程
一.数据库全备,全备脚本如下: [root@leader script]# cat bak_all.sh #!/bin/bash#Date: 2019-12-08#Author: chan#Mail: ...
- python3 基本数据类型转换
'''基本数据类型1:虽然python中的变量不需要声明,但使用时必须赋值 1.整形变量 2.浮点型变量 3.字符型2:可以一个给多个变量赋值,也可以多个给多个变量赋值3:python3中有6个标准数 ...
- QTableWidget懒加载(动态加载)实现
在QTableWidget中加入一行行的数据,如果数据数量过多,滚动起来就会卡顿,插入数据时也会影响性能,为了解决这个问题,采用懒加载的方式,只在界面上显示10几条数据,填满界面.同时增加而外的竖直滚 ...
- vue绑定样式
使用三目运算符绑定样式 本来以为使用vue模版写法,在绑定单个样式,也就是一个class类名的时候可以直接书写,就像这样 <div id="app"> <div ...
- 用grep来查询日志
转自:http://www.itokit.com/2013/0308/74883.html linux系统中,利用grep打印匹配的上下几行 如果在只是想匹配模式的上下几行,grep可以实现. ...
- LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
题目描述 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路 二叉树转化为链表的基本 ...
- tp5分页,一看就懂,简单明了(附带额外参数)
php 代码: $result = $jjModel->where($wheres)->paginate(10,false,['query' => ['peytype'=>$p ...
- 【gradle】【maven】gradle 转 maven pom.xml
在 对应的build.gradle 文件中加入以下代码,这里我是放在gradle文件的最开始位置: 这里是定义了一个task writeNewPom 来完成的. apply plugin: 'mave ...
- Laravel 中如何区别 Model 或者是 Builder?
User::where('id',1)->update([]) 和 User::find(1)->update([]) 有异曲同工之效. 额? 当你通过 Laravel 与数据库交 ...
- DatabaseLibrary数据库测试
DatabaseLibrary常用关键字 关 键 字 描 述 Connect To Database 连接数据库 Connect To Database Using Custom Params ...