简述题意,给你一课最小支撑树,对每个询问,在原有的路径上增加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的更多相关文章

  1. Codechef Dynamic Trees and Queries

    Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...

  2. Codeforces 1304E. 1-Trees and Queries 代码(LCA 树上两点距离判奇偶)

    https://codeforces.com/contest/1304/problem/E #include<bits/stdc++.h> using namespace std; typ ...

  3. Codeforces 1304E 1-Trees and Queries (树上距离+思维)(翻译向)

    题意 给你一棵树,q个询问(x,y,a,b,k),每次问你如果在(x,y)加一条边,那么a到b能不能走k步,同一个点可以走多次 思路(翻译题解) 对于一条a到b的最短路径x,可以通过左右横跳的方法把他 ...

  4. Codeforces 677C. Coloring Trees dp

    C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  5. codeforces 713A A. Sonya and Queries(状态压缩)

    题目链接: A. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input st ...

  6. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  7. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

  8. 【动态规划】Codeforces 711C Coloring Trees

    题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...

  9. [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: ...

随机推荐

  1. word写文档体会

    1.找一个文档规范要求. 2.根据文档的规范要求调整正文的格式,标题1的格式,标题2的格式,标题3的格式,图表的格式,把没用的那些格式都删除掉. 3.图注表注后空格一行. 4.设置页眉页脚. 5.生成 ...

  2. oracle 基础sql语句

    修改date日期时间: update T2_FOODS_STORAGE_IN set create_time =to_date('2020-01-15 12:30:20','yyyy-mm-dd hh ...

  3. 封装ajax库,post请求

    http状态码406是服务器无法根据客户端请求的内容特性完成请求 //整站功能方法库封装ajax请求,这里只针对post var methods = { //全站ajax请求状态处理 ajax: fu ...

  4. Java将数据进行分组处理

    将传人的数据进行分组,使用map保存每组的数据. /** * 将取出的数据进行分组 * @param list * @return */ public Map<Integer,Object> ...

  5. Genetic Algorithm 资源

    算法源码: NeuralGenetic : https://github.com/ahmedfgad/NeuralGenetic evolving-simple-organisms : https:/ ...

  6. pycharm新建Django时,遇到的坑,安装index包失败

    https://blog.csdn.net/li93675/article/details/89418097 如果在pycharm中导入django包 ,只对当前项目有效,建议使用命令pip inst ...

  7. Django 实现下载功能时中文文件名问题

    先上最终解决代码(有待验证各浏览器效果): def download_file(request, file_path): file_name = os.path.basename(file_path) ...

  8. python学习Day25--正则表达式1

    [知识点] # 所有模块要经历的两个步骤: ① 要操作的概念本身:正则表达式   时间 ② 使用模块取操作它:re                  time 1.正则表达式:一种匹配字符串的规则 # ...

  9. jquery--获取input radio单选框的值

    html <input type="radio" name="sex" value="man" checked> man < ...

  10. 【笔记7-部署发布】从0开始 独立完成企业级Java电商网站开发(服务端)

    阿里云服务 购买 连接 购买域名 域名备案 域名解析 源配置步骤 资源地址 http://learning.happymmall.com/ 配置阿里云的yum源 1.备份 mv /etc/yum.re ...