Codeforces 1304E. 1-Trees and Queries
简述题意,给你一课最小支撑树,对每个询问,在原有的路径上增加x-y,问a-b是否有路径长度为k的路,每条路每个点可以重复使用
由于是最小支撑树,我们可以用LCA来快速判断每个点之间的距离,那么现在就要判断情况,假设从原有的路上,a-b的距离为d,d=k时显然成立,当d<k时,若(d-k)%2=0也成立,因为若其是2的倍数,他可以轮流进入b与b的前驱,最后停在b上,那么我们如何判断新加的边的,距离的判定和前者一样,但是距离的大小变化了,设(x,y)表示x到y的最短路径,则从a到b就有三种情况
1.(a,b) 该情况已经讨论过
2.(a,x)+(b,y)+1,意思从a到x,x到y的路径长度为1,y再到b
3.(a,y)+(b,x)+1,同理
三种情况下的距离判断都一样,具体上代码看一下就懂了
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; const int maxm = 1e5+;
struct Node {
int v, nex;
} edges[maxm<<]; int head[maxm<<], edgecnt, grand[maxm][], depth[maxm], limit; void addedge(int u, int v) {
edges[++edgecnt] = {v, head[u]};
head[u] = edgecnt;
} void dfs(int u, int fa) {
depth[u] = depth[fa] + ;
grand[u][] = fa;
for(int i = ; i <= limit; ++i) grand[u][i] = grand[grand[u][i-]][i-];
for(int i = head[u]; i; i = edges[i].nex) {
int v = edges[i].v;
if(v != fa) dfs(v, u);
}
} int lca(int a, int b) {
if(a == b) return a;
if(depth[a] > depth[b]) swap(a, b);
for(int i = limit; i >= ; i--)
if(depth[a] <= depth[b] - (<<i)) b = grand[b][i];
if(a == b) return a;
for(int i = limit; i >= ; --i) {
if(grand[a][i] == grand[b][i])
continue;
else {
a = grand[a][i], b = grand[b][i];
}
}
return grand[a][];
} int getdist(int a, int b) {
int c = lca(a, b);
return depth[a] + depth[b] - *depth[c];
} void run_case() {
int n; cin >> n;
limit = floor(log(n+0.0) / log(2.0)) + ;
int u, v;
for(int i = ; i < n-; ++i) {
cin >> u >> v;
addedge(u, v), addedge(v, u);
}
dfs(, );
int q; cin >> q;
int a, b, x, y, k;
while(q--) {
cin >> x >> y >> a >> b >> k;
int dist = getdist(a, b);
bool flag = false;
if(dist <= k && (k-dist)%==) flag = true;
dist = getdist(a, x) + getdist(b, y) + ;
if(dist <= k && (k-dist)%==) flag = true;
dist = getdist(a, y) + getdist(b, x) + ;
if(dist <= k && (k-dist)%==) flag = true;
if(flag) cout << "YES\n";
else cout << "NO\n";
}
} int main() {
ios::sync_with_stdio(false), cin.tie();
//cout.setf(ios_base::showpoint);cout.precision(10);
//int t; cin >> t;
//while(t--)
run_case();
cout.flush();
return ;
}
Codeforces 1304E. 1-Trees and Queries的更多相关文章
- Codechef Dynamic Trees and Queries
Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...
- Codeforces 1304E. 1-Trees and Queries 代码(LCA 树上两点距离判奇偶)
https://codeforces.com/contest/1304/problem/E #include<bits/stdc++.h> using namespace std; typ ...
- Codeforces 1304E 1-Trees and Queries (树上距离+思维)(翻译向)
题意 给你一棵树,q个询问(x,y,a,b,k),每次问你如果在(x,y)加一条边,那么a到b能不能走k步,同一个点可以走多次 思路(翻译题解) 对于一条a到b的最短路径x,可以通过左右横跳的方法把他 ...
- Codeforces 677C. Coloring Trees dp
C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- codeforces 713A A. Sonya and Queries(状态压缩)
题目链接: A. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input st ...
- codeforces 375D:Tree and Queries
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- codeforces 711C Coloring Trees(DP)
题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...
- 【动态规划】Codeforces 711C Coloring Trees
题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...
- [Codeforces 863D]Yet Another Array Queries Problem
Description You are given an array a of size n, and q queries to it. There are queries of two types: ...
随机推荐
- Etcd Learning Notes
官网:https://etcd.io 官方项目地址:https://github.com/etcd-io/etcd 参考资料: https://www.hi-linux.com/posts/40915 ...
- Abaqus脚本接口及简单应用
目录 1.脚本接口简介 2. 宏录制 3. 宏回放 4. 宏编辑 5. 宏控制 1.脚本接口简介 Abaqus中的脚本接口(ASI)是在Python应用程序的基础上开发的,基于Abaqus中的脚本接口 ...
- 炸鱼w咕咕咕
退役选手又回来了w + 将近一个月莫有学OI变得十分笨拙 + 洛谷打卡从300+ 变为 10 + 冬令营加油(虽然我会崩 + 省选加油 (虽然我进不去
- AtCoDeer and Election Report
问题 G: AtCoDeer and Election Report 时间限制: 1 Sec 内存限制: 128 MB[提交] [状态] 题目描述 AtCoDeer the deer is seei ...
- DreamWeaver CC 中的回车
在Dreamweaver CC中换行有两种: 第一种是在设计视图中直接回车,对应的代码是<p>标签,即新生成一个段落. (注:在DreamWeaver CC编辑的代码中,按下回车相当于 ) ...
- canvas的beginPath和closePath
https://www.cnblogs.com/xuehaoyue/p/6549682.html https://segmentfault.com/a/1190000010330319 https:/ ...
- oracle查询表统计行数与注释
SELECT TABLE_NAME,NUM_ROWS,(select COMMENTS from user_tab_comments WHERE TABLE_NAME=C.TABLE_NAME) FR ...
- Ubuntu18 mongodb 离线安装
环境 Ubuntu 18 + mongodb 4.0.10 1.下载版本所需库 https://www.mongodb.com/download-center/community https://re ...
- 【代码学习】PYHTON 元组
Python的元组与列表类似,不同之处在于元组的元素不能修改.也可进行分片 和 连接操作. 元组使用小括号,列表使用方括号. 一.访问元组 #coding=utf-8 Tuple = ('name', ...
- WLC Crash采集什么信息?
WLC和思科的路由器交换机不同,Cisco的WLC采用的是AireOS. 如果WLC crash或无故重启,可以尝试采集如下信息: AireOS WLC version 8.0.140.0 or hi ...