E - 1-Trees and Queries

文章目录[题目链接:E - 1-Trees and Queries](https://codeforces.com/contest/1304/problem/E)题目大意解题思路代码题目链接:E - 1-Trees and Queriestime limit per test:4 seconds memory limit per test:512 megabytesinputstandar...
摘要由CSDN通过智能技术生成

题目链接:E - 1-Trees and Queries

time limit per test:4 seconds
memory limit per test:512 megabytes
inputstandard input
outputstandard output

Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?

Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wanted to see if similar techniques in trees can be used in 1-trees as well. Instead of solving it by himself, he’s going to test you by providing queries on 1-trees.

First, he’ll provide you a tree (not 1-tree) with n vertices, then he will ask you q queries. Each query contains 5 integers: x, y, a, b, and k. This means you’re asked to determine if there exists a path from vertex a to b that contains exactly k edges after adding a bidirectional edge between vertices x and y. A path can contain the same vertices and same edges multiple times. All queries are independent of each other; i.e. the added edge in a query is removed in the next query.

Input
The first line contains an integer n (3≤n≤105), the number of vertices of the tree.

Next n−1 lines contain two integers u and v (1≤u,v≤n, u≠v) each, which means there is an edge between vertex u and v. All edges are bidirectional and distinct.

Next line contains an integer q (1≤q≤105), the number of queries Gildong wants to ask.

Next q lines contain five integers x, y, a, b, and k each (1≤x,y,a,b≤n, x≠y, 1≤k≤109) – the integers explained in the description. It is guaranteed that the edge between x and y does not exist in the original tree.

Output
For each query, print “YES” if there exists a path that contains exactly k edges from vertex a to b after adding an edge between vertices x and y. Otherwise, print “NO”.
You can print each letter in any case (upper or lower).

input

5
1 2
2 3
3 4
4 5
5
1 3 1 2 2
1 4 1 3 2
1 4 1 3 3
4 2 3 3 9
5 2 3 3 9

output

YES
YES
NO
YES
NO

Note
The image below describes the tree (circles and solid lines) and the added edges for each query (dotted lines).
Possible paths for the queries with “YES” answers are:
1-st query: 1 – 3 – 2
2-nd query: 1 – 2 – 3
4-th query: 3 – 4 – 2 – 3 – 4 – 2 – 3 – 4 – 2 – 3

题目大意

给你一个数组 N 表示有 N 个节点,这些节点构成一棵树,然后输入N-1条边,接下来给个 M 有M次询问每次给出 x,y,a,b,k。这几个数分表表示 x 到 y 的距离为1,问你从否从a出发经过k步到达b,如果能就输出YES 否则就是NO

解题思路

我们可以确认的是,如果只要a到b的最短距离小于等于k且他们的奇偶性相同 (这里如果不懂的话其实画下就行了),那么肯定可以完成的,关键是都了x到y这条边,那么该如何处理呢?其实自习想想也就只不过是多出了两条路罢了 a—>x—>y—>b和a—>y—>x—>b,并且x和y之间的距离是1,那么我们只用考虑着三种情况就行了。至于最短距离我用的是倍增来求的。

代码


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值