E. 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

Copy

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

Copy

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).

1c2872e93b3768065463225f0bd43e6bd1f54def.pnguploading.4e448015.gif转存失败重新上传取消

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 – 3

题意:

跟你n个点的树形图, 多次询问让你求加一条边后的距离是不是k

思路:我LCA求出最短距离看能否走到即希望走的步数大于最短步数且相互差是否为偶数因为偶数可以相当于走过来走过去

所有可能的步数都是偶数差的步数

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 100010;
int a[maxn];
struct edge {
    int t, nex;
}e[maxn << 1]; int head[maxn], tot;
int book[maxn];
vector<int>edg[maxn];
void add(int x, int y) {
    e[++tot].t = y;
    e[tot].nex = head[x];
    head[x] = tot;
}
int depth[maxn], fa[maxn][22], lg[maxn];
void dfs(int now, int fath) {
    fa[now][0] = fath; depth[now] = depth[fath] + 1;
    for(int i = 1; i <= lg[depth[now]]; ++i)
        fa[now][i] = fa[fa[now][i-1]][i-1];
    for(int i = head[now]; i; i = e[i].nex)
        if(e[i].t != fath) dfs(e[i].t, now);
}
int LCA(int x, int y) {
    if(depth[x] < depth[y]) swap(x, y);
    while(depth[x] > depth[y])
        x = fa[x][lg[depth[x]-depth[y]] - 1];
    if(x == y) return x;
    for(int k = lg[depth[x]] - 1; k >= 0; --k)
        if(fa[x][k] != fa[y][k])
            x = fa[x][k], y = fa[y][k];
    return fa[x][0];
}
int val[maxn];
void bfs(int x) {
    queue<int>Q;
    Q.push(x);
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for(int i = 0;i < edg[u].size();++i) {
            if(!book[edg[u][i]]) {
                book[edg[u][i]] = 1;
                val[edg[u][i]] = val[u] + 1;
                Q.push(edg[u][i]);
            }
        }    
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i < n;++i) {
        int u ,v;
        scanf("%d%d", &u, &v);
        add(u, v);add(v, u);
        edg[u].push_back(v);
        edg[v].push_back(u);
    }
    for(int i = 1; i <= n; ++i) {
        lg[i] = lg[i-1] + (1 << lg[i-1] == i);
    }
    dfs(1, 0);
    val[1] = 0;book[1] = 1;
    bfs(1);
    int q;
    scanf("%d", &q);
    while(q--) {
        int x, y, a, b, k;
        scanf("%d%d%d%d%d", &x, &y, &a, &b, &k);
        int u1 = LCA(a, b);
        int u2 = LCA(a, x);int u3 = LCA(b, y);
        int u4 = LCA(a, y);int u5 = LCA(b, x);
        // printf("%d %d %d %d %d\n", u1, u2, u3, u4, u5);
        // for(int i = 1;i <= 5;++i) {
        //     printf("%d ", val[i]);
        // }
        // putchar('\n');
        int ans1 = val[a] + val[b] - 2 * val[u1];
        int ans2 = val[x] + val[a] - 2 * val[u2] + 1 + val[y] + val[b] - 2 * val[u3];
        int ans3 = val[y] + val[a] - 2 * val[u4] + 1 + val[b] + val[x] - 2 * val[u5];
        if(ans1 <= k && (k - ans1) % 2 == 0) {
            printf("YES\n");
        }
        else if(ans2 <= k && (k - ans2) % 2 == 0) {
            printf("YES\n");
        }
        else if(ans3 <= k && (k - ans3) % 2 == 0) {
            printf("YES\n");
        }
        else {
            printf("NO\n");
        }
    }
    return 0;
}
发布了219 篇原创文章 · 获赞 43 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_43568078/article/details/104432628