来自FallDream的博客,未经允许,请勿转载,谢谢。


给定一棵有n个节点的树,相邻两点之间的距离为1。
请找到一个点x,使其满足所有m条限制,其中第i条限制为dist(x,a[i])+dist(x,b[i])<=d[i]。
n<=300000,m<=300000
 
一个点如果满足题意,那么他和点1的距离至少为$max(0,\frac{ai+bi-di}{2})$
找出最大的那个点。如果有解,那么那个点肯定可以。暴力判断行不行就可以了。
#include<iostream>
#include<cstdio>
#define MN 300000
#define getchar() (*S++)
char B[<<],*S=B;
using namespace std;
inline int read()
{
int x = ; char ch = getchar();
while(ch < '' || ch > '') ch = getchar();
while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
return x;
}
int head[MN+],n,m,cnt=,dep[MN+],a[MN+],fa[MN+],b[MN+],d[MN+];
struct edge{int to,next;}e[MN*+];
inline void ins(int f,int t)
{
e[++cnt]=(edge){t,head[f]};head[f]=cnt;
e[++cnt]=(edge){f,head[t]};head[t]=cnt;
} void Dfs(int x,int f)
{
fa[x]=f;
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=f)
dep[e[i].to]=dep[x]+,Dfs(e[i].to,x);
} int main()
{
fread(B,,<<,stdin);
for(int T=read();T;--T)
{
n=read();m=read();cnt=;
for(register int i=;i<=n;++i) head[i]=;
for(register int i=;i<n;++i) ins(read(),read());
dep[]=;Dfs(,);int mx=,From=;
for(register int i=;i<=m;++i)
a[i]=read(),b[i]=read(),d[i]=read(),
(dep[a[i]]+dep[b[i]]-d[i]+)/>mx?(mx=(dep[a[i]]+dep[b[i]]-d[i]+)/,From=i):;
for(From=a[From];dep[From]>mx;From=fa[From]);
dep[From]=;Dfs(From,);bool flag=;
for(register int i=;i<=m;++i)
if(dep[a[i]]+dep[b[i]]>d[i]) {flag=;break;}
if(flag) printf("TAK %d\n",From);
else puts("NIE");
}
return ;
}

[bzoj4151][AMPPZ2014]The Cave的更多相关文章

  1. BZOJ.4151.[AMPPZ2014]The Cave(结论)

    BZOJ 不是很懂他们为什么都要DFS三次.于是稳拿Rank1 qwq. (三道题两个Rank1一个Rank3效率是不是有点高qwq?) 记以\(1\)为根DFS时每个点的深度是\(dep_i\).对 ...

  2. XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--J.Cave

    给你一棵树,现在有m个专家,每个专家计划从$a_i$走到$b_i$, 经过的距离不超过$d_i$,现在让你找一个点,使得所有专家的路途都能经过这个点 令$S_i$表示满足第i个专家的所有点,先检查1可 ...

  3. AMPPZ2014

    [AMPPZ2014]The Lawyer 记录每天结束的最早的会议以及开始的最晚的会议即可. #include<cstdio> #define N 500010 int n,m,i,d, ...

  4. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  5. [BZOJ4151]The Cave

    Solution: ​ 假设现在在点1,有许多形如 (x, y, z) 的限制条件,那么对于一组限制,必须先走到 x, y 的 \(\frac{z-dis(x, y)}{2}\) 级祖先,叫这些点为限 ...

  6. BZOJ 4144: [AMPPZ2014]Petrol

    4144: [AMPPZ2014]Petrol Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 457  Solved: 170[Submit][Sta ...

  7. 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测 LCT/并查集

    两种方法: 1.LCT 第一次LCT,只有link-cut和询问,无限T,到COGS上找了数据,发现splay里的父亲特判出错了(MD纸张),A了,好奇的删了反转T了.... #include < ...

  8. 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 link-cut-tree

    2016-05-30  11:04:51 学习了link-cut-tree 二中神犇封禹的讲义感觉讲的超级清晰易懂啊(没有的可以q窝 算是模板吧 #include<bits/stdc++.h&g ...

  9. 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain

    循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...

随机推荐

  1. VS系列控制台闪退解决

    查阅--->总结-->实践--> 按红色标识走 ,完美解决! 至此,完美解决:原理不深究:

  2. Struts2之配置

    Struts2的默认配置文件是struts.xml放在/web-inf/classes目录下,struts配置文件的最大作用就是配置Action与请求之间的对应关系,并配置逻辑视图名和物理视图名之间的 ...

  3. 2017 清北济南考前刷题Day 4 afternoon

    期望得分:30+50+30=110 实际得分:40+0+0=40 并查集合并再次写炸... 模拟更相减损术的过程 更相减损术,差一定比被减数小,当被减数=减数时,停止 对于同一个减数来说,会被减 第1 ...

  4. LeetCode & Q53-Maximum Subarray-Easy & 动态规划思路分析

    Array DP Divide and Conquer Description: Find the contiguous subarray within an array (containing at ...

  5. 快速搭建ssm框架

    快速搭建SSM框架 因为最近有很多朋友问我自己的项目搭建的不够完善,并且经常出现一些小问题,那么今天我又整理了一下文档教大家如何快速搭建SSM框架我是用 eclipse搭建的,如果想用idear的话我 ...

  6. springboot多模块项目下,子模块调用报错:程序包xxxxx不存在

    今天在用springboot搭建多模块项目,结构中有一个父工程Parent  一个通用核心工程core 以及一个项目工程A 当我在工程A中引入core时,没有问题,maven install正常 当我 ...

  7. 新概念英语(1-32)A fine day

    新概念英语(1-33)A fine day Where is the Jones family? It is a fine day today. There are some clouds in th ...

  8. bootstrap 菜单之手风琴效果

    自己用bootstrap搭了个项目,纯属娱乐....为了检验学习bootstrap之成果. 效果如图: 一.搭建中发现一问题,因为以前测试都是写的html页面,这次用了母版页,点击页面的之后,页面会刷 ...

  9. JSON(四)——异步请求中前后端使用Json格式的数据进行交互

    json格式的数据广泛应用于异步请求中前后端的数据交互,本文主要介绍几种使用场景和使用方法. 一,json格式字符串 <input type="button" id=&quo ...

  10. C++ 排列最优解算法思想

    枚举全排列 #include <iostream> #include <cstring> #include <string> using namespace std ...