最短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种染色方法 在一种染色方法中 ...
随机推荐
- JQuery操作DOM(8)
一.jQuery操作样式 1.设置和获取样式 /* 单个样式 */ $(selector).css(属性,值): /* 多个样式 */ $(selector).css({属性:值,属性:值}); /* ...
- Java并发编程(十一)——原子操作CAS
一.原子操作 syn基于阻塞的锁的机制,1.被阻塞的线程优先级很高,2.拿到锁的线程一直不释放锁怎么办?3.大量的竞争,消耗cpu,同时带来死锁或者其他安全. CAS的原理 CAS(Compare A ...
- flask读书记录
1. 在flask中,如果我们在视图函数中使用data = request.get_json()方法获取数据,那么在客户端发送POST请求时,就需要设置设置正确的Content-Type首部.在aja ...
- Retrofit 使用简介
一,简介 Retrofit 是目前使用广泛的 Http Client 框架,它适用于 Android 和 Java. 但需要注意的是,Retrofit 本身并不是一个网络请求框架,而是一个网络请求框架 ...
- (转载)Zookeeper的功能以及工作原理
本文转载自:https://www.cnblogs.com/felixzh/p/5869212.html 1.ZooKeeper是什么? ZooKeeper是一个分布式的,开放源码的分布式 ...
- 关于wordpress文章分类显示404错误的解决办法。
闲来无事,在虚拟主机上装了一个wordpress尝试自己搭一个博客玩一下,发现文章分类一直显示404错误,网上查了好久,终于找到解决方法,其实很简单,只要将分类的别名改成英文的就解决了,分类中不能包含 ...
- TCP之Nagle算法与TCP_NODELAY
1. Nagle 算法 在一个 Rlogin 连接上客户一般每次发送一个字节到服务器,这就产生了一些 41 字节长的分组:20 字节的 IP 首部.20 字节的 TCP 首部和 1 个字节的数据.在局 ...
- LeetCode 216. 组合总和 III(Combination Sum III)
题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. 示例 1: 输入 ...
- laravel 中first和find区别(总结一)
检索单个模型/集合 除了从指定的数据表检索所有记录外,你也可以通过 find 或 first 方法来检索单条记录.这些方法不是返回一组模型,而是返回一个模型实例: // 通过主键取回一个模型... $ ...
- Socket——实现一个简单的静态网页服务器
整体结构就是使用ServerSocket监听一个地址,当有接受到请求之后,解析请求中的资源路径.服务器资源路径存放在项目下的一个目录中,服务器会到这个目录中根据请求的路径去寻找相应的资源.如果找到了则 ...