传送门

题目大意

给你一棵无根树,然后询问Q次,每次把点$x$和点$y$连接,问你从点$a$到点$b$是否有一条长度为$k$的简单路径,每次询问完后会把新添加的边删除。

思路:树上LCA

题目跟2019pjt4很像,可以说这个就是那道题的树上版本。

因为每次讯问完都会把新添的边删去,所以我们只要想如何处理添完一条边后$a$到$b$的简单路径。

所以我们可以分两种情况讨论:

    1. 1.把$\left ( x,y \right )$算进$\left ( a,b \right )$的路径。
    2. 2.只算$\left ( a,b \right )$的路径。

简单来说就是求a->x->y->b或a->y->x->b或a->b的路径,众所周知树上两点之间的路径长度可以用LCA来求然后就没了。

然后我们想一下,如果$k$比路径长度大的话,我们就往父亲节点走,很容易发现,如果多出来的部分如果不是2的倍数的话,那么就不存在一条长度为$k$的路径。

不理解的可以自己画一下图。

代码

#include <bits/stdc++.h>

#define RI register int

using namespace std;

template <class T>
inline void read(T &x) {
T f = 1; x = 0; char c = getchar();
while(c > '9' || c < '0') {
if(c == '-')
f = -f;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
} const int N = 1e5 + 7;
int n;
struct Edge {
int nxt, to;
} edge[N << 1];
int head[N], tot;
int dep[N], st[N][21]; inline void add(int x, int y) {
edge[++tot].nxt = head[x];
edge[tot].to = y;
head[x] = tot;
} inline void Read() {
read(n);
for(RI i = 1; i < n; i++) {
int x, y;
read(x), read(y);
add(x, y);
add(y, x);
}
} inline void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
st[x][0] = fa;
for(RI i = 1; i <= 20; i++)
st[x][i] = st[st[x][i - 1]][i - 1];
for(RI i = head[x]; i; i = edge[i].nxt) {
int y = edge[i].to;
if(y == fa)
continue;
dfs(y, x);
}
} inline int Lca(int x, int y) {
if(dep[y] > dep[x])
swap(x, y);
for(RI i = 20; i >= 0; i--)
if(dep[st[x][i]] >= dep[y])
x = st[x][i];
if(x == y)
return x;
for(RI i = 20; i >= 0; i--)
if(st[x][i] != st[y][i])
x = st[x][i], y = st[y][i];
return st[x][0];
} int main() {
Read();
dfs(1, 0);
int T;
read(T);
while(T--) {
int x, y, a, b, k;
int res1, res2, res3;
read(x), read(y), read(a), read(b), read(k);
int Lca1 = Lca(a, b), Lca2_1 = Lca(a,x), Lca2_2 = Lca(y,b), Lca3_1 = Lca(a,y), Lca3_2 = Lca(x,b); res1 = dep[a] + dep[b] - 2 * dep[Lca1];
res2 = dep[a] + dep[x] + dep[y] + dep[b] - 2 * dep[Lca2_1] - 2 * dep[Lca2_2] + 1;
res3 = dep[a] + dep[x] + dep[y] + dep[b] - 2 * dep[Lca3_1] - 2 * dep[Lca3_2] + 1; if(res1 <= k && (k - res1) % 2 == 0) {
puts("YES");
continue;
}
if(res2 <= k && (k - res2) % 2 == 0) {
puts("YES");
continue;
}
if(res3 <= k && (k - res3) % 2 == 0) {
puts("YES");
continue;
}
puts("NO");
}
return 0;
}

题解-------CF1304E 1-Trees and Queries的更多相关文章

  1. 题解 CF1304E 【1-Trees and Queries】

    前言 这场比赛,在最后 \(5\) 分钟,我想到了这道题的 \(Idea\),但是,没有打完,比赛就结束了. 正文 题目意思 这道题目的意思就是说,一棵树上每次给 \(x\) 和 \(y\) 节点连 ...

  2. Codechef Dynamic Trees and Queries

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

  3. 题解-Codeforces917D Stranger Trees

    Problem \(\mathrm{Codeforces~917D}\) 题意概要:一棵 \(n\) 个节点的无向树.问在 \(n\) 个点的完全图中,有多少生成树与原树恰有 \(k\) 条边相同,对 ...

  4. 【题解】Luogu CF817F MEX Queries

    原题传送门 817,我突然想到了某8位质数 这题珂以说是珂朵莉树的模板 三个操作都肥肠简单,前两个区间赋值,第三个区间0变1,1变0 每次输出从头开始扫描就行(我忘了珂朵莉树的性质,竟然还动态维护最左 ...

  5. LeetCode题解之Leaf-Similar Trees

    1.题目描述 2.问题分析 将叶子节点的值放入vector,然后比较. 3.代码 bool leafSimilar(TreeNode* root1, TreeNode* root2) { vector ...

  6. [CodeChef-ANUDTQ] Dynamic Trees and Queries

    类似维护括号序列,给每个点建两个点,然后所有操作都能轻松支持了.注意sum和lastans是long long. #include<cstdio> #include<algorith ...

  7. 题解 CF375D 【Tree and Queries】

    首先,子树上的查询问题可以通过$DFS$序转为序列问题 再一看,没有修改,可以离线,这不就是莫队吗? 我们用$sum_i$表示出现次数$\geq i$的个数 用$val_i$表示第$i$种颜色的出现次 ...

  8. 题解 UVA1479 【Graph and Queries】

    \[ \text{Preface} \] 算是一道思维难度稍易,代码难度稍难的题吧. \[ \text{Description} \] 给出一张 \(n\) 个点,\(m\) 条边的图,点带权.需要支 ...

  9. 题解 CF938G 【Shortest Path Queries】

    题目让我们维护一个连通无向图,边有边权,支持加边删边和询问从\(x\)到\(y\)的异或最短路. 考虑到有删边这样的撤销操作,那么用线段树分治来实现,用线段树来维护询问的时间轴. 将每一条边的出现时间 ...

随机推荐

  1. TX2_安装view_team

    TX2上的帐号是:1317149963,dc200820305233 参考网站:https://blog.csdn.net/qq_33512213/article/details/90050792 安 ...

  2. SASS - 语法

    SASS – 简介 SASS – 环境搭建 SASS – 使用Sass程序 SASS – 语法 SASS – 变量 SASS- 局部文件(Partial) SASS – 混合(Mixin) SASS ...

  3. java使用mongoTemplate去重排序查询

    import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.c ...

  4. Linux 目录变化监听 - python代码实现

    在python中 文件监控主要有两个库, 一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),pyinotify依赖于Linux平台的in ...

  5. SpringCloud学习之Hystrix请求熔断与服务降级(六)

    我们知道大量请求会阻塞在Tomcat服务器上,影响其它整个服务.在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败.高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险 ...

  6. lvm 逻辑卷分区删除恢复

    原因:执行 lvremove /dev/system/lv_trans 删除逻辑分区 恢复: 1.进入到lvm查看元数据 cd /etc/lvm/archive 2.恢复元vg卷组 vgcfgrest ...

  7. Cracking Digital VLSI Verification Interview 第一章

    目录 Digital Logic Design Number Systems, Arithmetic and Codes Basic Gates Combinational Logic Circuit ...

  8. Spring Cloud Alibaba 教程 | 前世今生

    Spring Cloud Alibaba是什么 先来看一下官方是怎么定义Spring Cloud Alibaba的: Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案.此 ...

  9. Mac OS/Windows好用软件分享

    下软件全部为破解版,仅供参考学习用,如涉及商业. 请支持正版!谢谢 全部为本人亲测过 看上哪个留言发给你!   直接全分享上来会有人居心不良!

  10. 第一次 C语言课程设计

    小学生测验 最近比较忙,就拿前几天做的课设项目水一水. 内容 面向小学1~2年级学生,随机选择两个整数和加减法形成算式要求学生解答. 功能要求: (1)电脑随机出10道题,每题10分,程序结束时显示学 ...