简述题意,给你一课最小支撑树,对每个询问,在原有的路径上增加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. JSAJAX请求

    let xmlHttp = new XMLHttpRequest();//创建发送请求的对象     //这是为了兼容IE的获取ajac请求对象的方法     // let getXmlHttpReq ...

  2. Bug搬运工-CSCvf74866:Webauth scaling improvements - fix problem with GUI going unresponsive with HTTPS redirect

    Webauth scaling improvements - fix problem with GUI going unresponsive with HTTPS redirect CSCvf7486 ...

  3. 深入理解Java虚拟机(1)

    Java内存区域 对于Java程序员来说,在虚拟机的自动内存管理机制下,不再需要为每一个new操作去写配对的delete和free代码,不容易出现内存泄露和内存溢出问题,可以直接交给虚拟机进行管理. ...

  4. cc攻击怎么防御,如何防止cc攻击?

    当我们访问一个网站时,如果网站页面越简单,访问速度越快,页面越漂亮,加载速度就越慢,因为要加载更多东西,服务器压力也会比较大.cc攻击就是利用这种弱点,使用大量代理服务器,对网站进行攻击,消耗网站服务 ...

  5. json 字符串 <----> json 对象

    一,字符串 -->JSON对象 1,转换函数 JSON.parse(json_str): 2,$.parseJSON(json_str):  用的是 jquery 的插件  所以需要引入 jq. ...

  6. ubuntu 18.04中nginx不能访问软链接目录中的内容

    解决办法:以root权限运行nginx,即修改/etc/nginx/nginx.conf中的user www-data为root

  7. oracle 提取文本中的数字

    提取文本中的数字部分,并转换为数字 TO_NUMBER(regexp_substr(AGE,'[0-9.]+'))

  8. Java入门笔记 09-集合

    一.Collection接口方法:Collection 接口是 List.Set 和 Queue 接口的父接口,该接口里定义的方法既可用于操作 Set 集合,也可用于操作 List 和 Queue 集 ...

  9. POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)

    放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> ...

  10. lc 0224

    目录 ✅ 766. 托普利茨矩阵 描述 解答 cpp py ✅ 566. 重塑矩阵 描述 解答 java py ✅ 637. 二叉树的层平均值 描述 解答 cpp py java 0224 algo ...