CodeForces - 1304E H - 1-Trees and Queries (LCA)

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 nn vertices, then he will ask you qq queries. Each query contains 55 integers: xx, yy, aa, bb, and kk. This means you're asked to determine if there exists a path from vertex aa to bb that contains exactly kk edges after adding a bidirectional edge between vertices xx and yy. 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 nn (3≤n≤1053≤n≤105), the number of vertices of the tree.

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

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

Next qq lines contain five integers xx, yy, aa, bb, and kk each (1≤x,y,a,b≤n1≤x,y,a,b≤n, x≠yx≠y, 1≤k≤1091≤k≤109) – the integers explained in the description. It is guaranteed that the edge between xx and yy does not exist in the original tree.

Output

For each query, print "YES" if there exists a path that contains exactly kk edges from vertex aa to bb after adding an edge between vertices xx and yy. Otherwise, print "NO".

You can print each letter in any case (upper or lower).

Example

 

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:

  • 11-st query: 11 – 33 – 22
  • 22-nd query: 11 – 22 – 33
  • 44-th query: 33 – 44 – 22 – 33 – 44 – 22 – 33 – 44 – 22 – 33

Sponsor

一道lca的模板题

判断给的k和求出的三种路径中是否有奇偶性相同的且小于k的

有就yes无就no

#include<bits/stdc++.h>
const int maxn = 5e5 + 10;
int n, m, s;
int cnt;
int tot;
int head[maxn];
int depth[maxn * 2];
int order[maxn * 2];
int st[maxn * 2][20];
int fp[maxn * 2];
int lg[maxn * 2];
int dis[maxn * 2];
using namespace std;

struct node{
	int to;
	int next;
	node() {}
	node(int a, int b) : to(a), next(b) {}
}edge[maxn * 2];

void edgeadd(int a, int b){
	edge[tot] = node(b, head[a]);
	head[a] = tot++;
	edge[tot] = node(a, head[b]);
	head[b] = tot++;
}

void init(){
	memset(head, -1, sizeof(head));
	memset(depth, 0, sizeof(depth));
	memset(order, 0, sizeof(order));
	memset(st, 0, sizeof(st));
	memset(fp, 0, sizeof(fp));
	tot = 0;
	cnt = 0;
}

void dfs(int nw, int dpt){
	++cnt;
	fp[nw] = cnt;
	order[cnt] = nw;
	depth[cnt] = dpt + 1;

	for(int i = head[nw]; i != -1; i = edge[i].next){
		int v = edge[i].to;
		if(!fp[v]){
            dis[v] = dis[nw] + 1;
			dfs(v, dpt + 1);
			++cnt;
			order[cnt] = nw;
			depth[cnt] = dpt + 1;
		}
	}
}

void ST_init(){
	for(int i = 1; i <= cnt; i++)
		st[i][0] = i;
	int k = lg[cnt];
	for(int j = 1; j <= k; j++){
		for(int i = 1; i + (1 << j) - 1 <= cnt; i++){
			int a = st[i][j - 1];
			int b = st[i + (1 << (j - 1))][j - 1];
			if(depth[a] < depth[b])
				st[i][j] = a;
			else
				st[i][j] = b;
		}
	}
}

int find_lca(int l, int r){
    int a = fp[l];
    int b = fp[r];
    if(a > b)
        swap(a, b);
    int k = lg[b - a];
    int x = st[a][k];
    int y = st[b - (1 << k) + 1][k];
    if(depth[x] < depth[y])
        return order[x];
    else
        return order[y];

}

int dis_lca(int l, int r){
    //cout << dis[l] + dis[r] - 2 * dis[find_lca(l, r)] << endl;
    if(l == r)
        return 0;
    return dis[l] + dis[r] - 2 * dis[find_lca(l, r)];
}

bool check(int x, int y){
    //cout << x << y << endl;
    if(x <= y && (x % 2 == y % 2))
        return 1;
    else
        return 0;
}

void solve(){
    cin >> n;
    init();
	lg[0] = -1;
	for(int i = 1; i <= maxn * 2; i++)
		lg[i] = lg[i >> 1] + 1;
    for(int i = 1; i < n; i++){
        int a, b;
        cin >> a >> b;
        edgeadd(a, b);
    }
    dis[1] = 0;
    dfs(1, 0);
    /*for(int i = 1; i <= n; i++)
        cout << dis[i] << endl;*/
    ST_init();
    cin >> m;
    for(int i = 1; i <= m; i++){
        int x, y, a, b, k;
        cin >> x >> y >> a >> b >> k;
        int a1 = dis_lca(a, b), a2 = dis_lca(a, x) + dis_lca(y, b) + 1, a3 = dis_lca(a, y) + dis_lca(x, b) + 1;
        //cout << a1 << " " << a2 <<  " " << a3 << endl;
        if(check(a1, k) ||
           check(a2, k) ||
           check(a3, k))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
}

int main(){
    solve();
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值