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: ...
随机推荐
- 一组关于{x}的积分
\[\Large\displaystyle \int_{0}^{1}\left \{ \frac{1}{x} \right \}\mathrm{d}x~,~\int_{0}^{1}\left \{ \ ...
- 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...
- 安卓之滚动视图ScrollView
(1)垂直方向滚动时,layout_width要设置为match_parent,layout_height要设置为wrap_content (2)水平方向滚动时,layout_width要设置为wra ...
- mui下拉刷新 ios click事件无法响应问题
使用mui的事件监听事件 tap mui("#pullrefresh").on('tap', '.ulDiv', function (event) {this.click();}) ...
- C++标准库里面没有字符分割函数split,自己编写函数实现字符串分割功能
#include <vector> #include <string> #include <iostream> using namespace std; vecto ...
- WebRTC的音频编码(转)
一.一个典型的IP通信模型 二.Server2Server技术分类 Server2Server这块也是一个专门的领域,这里只简单分个类. 1.同一国家相同运营商之间: 同一运营商之间也有丢包,在铁通, ...
- 最常用的CountDownLatch, CyclicBarrier你知道多少? (Java工程师必会)
CountdownLatch,CyclicBarrier是非常常用并发工具类,可以说是Java工程师必会技能了.不但在项目实战中经常涉及,而且在编写压测程序,多线程demo也是必不可少,所以掌握它们的 ...
- IDEA中使用Springboot+SSM的踩坑记(一)
今天由于电脑无限蓝屏,不知怎么把我IDEA里面破解过的一些东西给搞没了,包括IDEA本体和JRebel,照着原来的方法破解连本体都开不起来了(哭死),索性下了个最新版来用,结果JRebel还是破解不得 ...
- python网络爬虫之解析网页的XPath(爬取Path职位信息)[三]
目录 前言 XPath的使用方法 XPath爬取数据 后言 @(目录) 前言 本章同样是解析网页,不过使用的解析技术为XPath. 相对于之前的BeautifulSoup,我感觉还行,也是一个比较常用 ...
- 压力测试-apachebench
压力测试-apachebench 1. 压力测试 压力测试的概念\定义: 性能测试Performance Test :是指通过自动化的测试工具模拟多种正常.峰值以及异常负载条件来对系统的各项 ...