给你一棵树,现在有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. VC6中函数点go to definition报告the symbol XXX is undefined

    删除Debug中的bsc文件,再重建所有文件即可,在该函数处点击go to definition会提示重建.bsc文件,如果不行,多操作几次.

  2. 日常开发工作常用linux命令

    :wq 保存退出 :q! 强制退出 vi 查看 vim 编辑 rpm -qa|grep jdk 命令查看当前的jdk情况 yum -y remove java java-1.7.0-openjdk* ...

  3. CentOS7 配置ISCSI targetcli 共享存储

  4. 对比剖析Swarm Kubernetes Marathon编排引擎

    Docker Native Orchestration 基本结构 Docker Engine 1.12 集成了原生的编排引擎,用以替换了之前独立的Docker Swarm项目.Docker原生集群(S ...

  5. 安卓GreenDao(基础)

    GreenDao的基础使用很简单,网上一大筐,推荐在简书里面搜索,那么我这里要说些什么呢,试想,这些简单的Demo可以带你了解GreenDao,但你能用这些代码做公司的项目么,肯定不行,所以我结合自身 ...

  6. 【js】js声明与数据类型

    之前整理知识点感觉有点没有针对性,每期知识点之间都没有关联,不成体系,其实对学习与运用知识并无益,随着知识的积累,不使用就会忘记.所以从本次开始,将对知识点进行体系化.先列出本期知识体系图,再进行逐步 ...

  7. Laravel5.5学习笔记

    安装composer 下载安装脚本 php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php ...

  8. [c/c++] programming之路(29)、阶段答疑

    一.指针不等于地址 指针不仅有地址,还有类型,是一个存储了地址的变量,可以改变指向:而地址是一个常量 #include<stdio.h> #include<stdlib.h> ...

  9. 将markdown文档使用gulp转换为HTML【附带两套css样式】

    将markdown文档使用gulp转换为HTML[附带两套css样式] 今天遇到一个需求,即将Markdown文档转为为HTML在网页展示,身为一名程序员,能用代码解决的问题,手动打一遍无疑是可耻的. ...

  10. Domain logic approachs

    1.transaction script(事务脚本) 概述: 很多企业应用可以看成一系列的事务,每一个事务可以通过使用一个Transaction Script来处理. 用法: 使用Transactio ...