给你一棵树,现在有m个专家,每个专家计划从$a_i$走到$b_i$, 经过的距离不超过$d_i$,现在让你找一个点,使得所有专家的路途都能经过这个点

令$S_i$表示满足第i个专家的所有点,先检查1可不可以,不行的话,找到离根最远的专家i,找$S_i$中最靠近根的那个点

#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
typedef pair<int, int> P;
const int N = 3e5 + ;
int n, m;
vector<int> g[N];
struct Requirement {
int a, b, d;
}req[N];
int fa[N], dep[N];
void dfs(int u, int f) {
dep[u] = dep[f] + ;
fa[u] = f;
for (int v: g[u])
if (v != f) dfs(v, u);
}
int main() {
int T;
scanf("%d", &T);
while (T --) {
scanf("%d%d", &n, &m);
rep(i, , n) g[i].clear();
rep(i, , n - ) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
rep(i, , m) {
scanf("%d%d%d", &req[i].a, &req[i].b, &req[i].d);
}
auto calc = [&](int v, int i) -> int {
return (dep[req[i].a] + dep[req[i].b] - req[i].d + ) / ;
};
auto solve = [&]() -> int {
dep[] = -;
dfs(, );
int maxv = -, t;
rep(i, , m) {
int d = calc(, i);
if (d > maxv) {
maxv = d; t = i;
}
}
if (maxv <= ) return ;
int u = req[t].a;
rep(i, , dep[req[t].a] - maxv) u = fa[u];
dfs(u, );
maxv = -;
rep(i, , m) {
int d = calc(u, i);
if (d > maxv) maxv = d;
}
if (maxv <= ) return u;
return -;
};
int ans = solve();
if (ans < ) printf("NIE\n");
else printf("TAK %d\n", ans);
}
}
/*
2
5 3
1 2
2 3
2 4
3 5
1 4 2
5 5 5
3 2 1
*/

XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--J.Cave的更多相关文章

  1. XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--B.Petrol

    多源最短路+并查集 #include <bits/stdc++.h> using namespace std; #define rep(i, j, k) for (int i = int( ...

  2. XII Open Cup named after E.V. Pankratiev. GP of Eastern Europe (AMPPZ-2012)

    A. Automat $m$超过$1600$是没用的. 从后往前考虑,设$f[i][j][k]$表示考虑$[i,n]$这些物品,一共花费$j$元钱,买了$k$个物品的最大收益. 时间复杂度$O(n^5 ...

  3. XV Open Cup named after E.V. Pankratiev. GP of Tatarstan

    A. Survival Route 留坑. B. Dispersed parentheses $f[i][j][k]$表示长度为$i$,未匹配的左括号数为$j$,最多的未匹配左括号数为$k$的方案数. ...

  4. XV Open Cup named after E.V. Pankratiev. GP of America

    A. Area of Effect 首先最优解中必有一个点在圆的边界上. 若半径就是$R$,则枚举一个点,然后把剩下的事件极角扫描即可,时间复杂度$O(m(n+m)\log(n+m))$. 否则圆必然 ...

  5. XV Open Cup named after E.V. Pankratiev. GP of Three Capitals

    A. Add and Reverse 要么全部都选择$+1$,要么加出高$16$位后翻转位序然后再补充低$16$位. #include<stdio.h> #include<iostr ...

  6. XV Open Cup named after E.V. Pankratiev. GP of Siberia-Swimming

    给出两个点,找到过这两个点的等角螺线,并求出中间的螺线长 $c = \frac{b}{a}$ $p = a \times c^{\frac{\theta}{angle}}$ 对弧线积分 #includ ...

  7. XIX Open Cup named after E.V. Pankratiev. GP of Poland(AMPPZ-2018)

    A. Drone With a Camera 三分套三分. #include<cstdio> #include<cmath> #include<algorithm> ...

  8. XIV Open Cup named after E.V. Pankratiev. GP of Europe

    A. The Motorway 等价于找到最小和最大的$L$满足存在$S$使得$S+(i-1)L\leq a_i\leq S+i\times L$ 即 $S\leq\min((1-i)L+a_i)$ ...

  9. XV Open Cup named after E.V. Pankratiev Stage 6, Grand Prix of Japan Problem J. Hyperrectangle

    题目大意: 给出一个$d$维矩形,第i维的范围是$[0, l_i]$. 求满足$x_1 + x_2 + ...x_d \leq s$ 的点构成的单纯形体积. $d, l_i \leq 300$ 题解: ...

随机推荐

  1. 再谈javascriptjs原型与原型链及继承相关问题

    什么是原型语言 只有对象,没有类;对象继承对象,而不是类继承类. “原型对象”是核心概念.原型对象是新对象的模板,它将自身的属性共享给新对象.一个对象不但可以享有自己创建时和运行时定义的属性,而且可以 ...

  2. ngx-admin with Asp.net Core 2.0, possibly plus OrchardCore

    1 Download ngx-admin from https://github.com/akveo/ngx-admin 2 Create a new Web Application in vs201 ...

  3. spring boot + vue + element-ui全栈开发入门——项目部署

     前言 常用的部署方式有两种: 1.是把生成好的静态页面放到spring boot的static目录下,与打包后的spring boot项目一起发布,当spring boot运行起来后,自然而然就能访 ...

  4. pytorch使用不完全文档

    1. 利用tensorboard看loss: tensorflow和pytorch环境是好的的话,链接中的logger.py拉到自己的工程里,train.py里添加相应代码,直接能用. 关于环境,小小 ...

  5. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165335

    一.资源下载以及工具安装 1.下载虚拟机工具VMware. 下载链接 :https://www.baidu.com/link?url=uuaBW5ETUl3GrhUKvPbbEc7QlQvGHfpD8 ...

  6. 【转载】pycharm破解,可使用到2099年.pycharm版本 pycharm-professional-2016.3.1

    1. Pycharm的安装方法,论坛很多,这里就不赘述了.参照:http://blog.csdn.net/qq_29883591/article/details/52664478 2. 下载Pycha ...

  7. 初学javascript《一》break和continue的标签问题

    <script>var iNum = 0;outermost:for (var i=0; i<3; i++) {   for (var j=0; j<3; j++) {     ...

  8. linux下wrk的安装

    wrk是linux下开源的性能测试工具,并且只能在linux下运行,下面介绍下安装教程(以ubantu18.04环境为例): 1.预先安装git,如:apt install git 2.从git上拉取 ...

  9. Python数据分析Pandas库之熊猫(10分钟二)

    pandas 10分钟教程(二) 重点发法 分组 groupby('列名') groupby(['列名1','列名2',.........]) 分组的步骤 (Splitting) 按照一些规则将数据分 ...

  10. vxworks开发中simulator的使用之建立虚拟网卡

    在使用windriver workben ch开发vxWorks应用时,有时需要在本机上利用Simulator跑一下程序,这就需要你安装一个虚拟的网卡.vxWorks自带了这些工具,下面,以windo ...