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

    1.'\0'的ASCII码为0 2.用串口发送字符串时,可以通过'\0'判断字符串是否结束,但发送数字数组的时候不能通过'\0'判断数组是否结束,因为数字0与'\0'的ASCII码值相同.

  2. PLSQL Developer 连接多个数据库

    1.新建一个Oracle的dblink 2.打开客户端instantclient目录,可能不是和PLSQL Developer 一个目录,然后用记事本编辑tnsnames.ora 3.把链接粘贴进去, ...

  3. 简单的jquery Ajax进行登录!

    本案例包括login.html.login.php.jquery-1.12.0.min.js三个文件,只需将这三个文件放到同一文件夹下,即可运行. login.html: <!DOCTYPE h ...

  4. 【MySQL】存储引擎

    " 目录 #. MySQL支持的存储引擎 1. InnoDB 2. MyISAM 3. NDB 4. Memory 5. Infobright 6. NTSE 7. BLACKHOLE My ...

  5. gitlab的搭建与汉化

    gitlab的搭建:内存最好2G以上 yum -y install curl unzip policycoreutils git wget         安装相关依赖包 所有gitlab rpm包的 ...

  6. SpringCloud全家桶学习之断路器---Hystrix(五)

    目前我也在摸索着学习Spring Cloud,本节主要摸索的是服务熔断.服务降级.Hystrix服务监控. 一.Hystrix概述 (1)服务雪崩 服务雪崩:多个微服务之间调用的时候,假设微服务A调用 ...

  7. java中4种常用线程池

    一.线程池 线程池:说白了,就是一种线程使用模式.线程过多会带来调度开销,进而影响整体性能.而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务,这避免了在处理短时间任务时创建与销毁线程的代价 ...

  8. http的长连接和短连接(史上最通俗!)

    1.以前的误解 很久之前就听说过长连接的说法,而且还知道HTTP1.0协议不支持长连接,从HTTP1.1协议以后,连接默认都是长连接.但终究觉得对于长连接一直懵懵懂懂的,有种抓不到关键点的感觉. 今天 ...

  9. 解决STM32工程出现:Undefined symbol TIM_ClearFlag (referred from hcsr04.o).错误。类型问题Undefined symbol TIM_xxx (referred from xxx.o).

    出错原因: 工程FWLIB目录下没有添加stm32f10x_tim.c文件. 添加即可. 一般利用库开发,将ppp.c(ppp.c又调用了库stm32f10x_xx.h)写好之后的调用步骤: 举例使用 ...

  10. 服务器(1)——IIS(1)——Windows7中IIS简单安装与配置(详细图解)

    最近工作需要IIS,自己的电脑又是Windows7系统,找了下安装的方法,已经安装成功. 一.首先是安装IIS.打开控制面板,找到“程序与功能”,点进去 二.点击左侧“打开或关闭Windows功能” ...