# LCA codeforces 100685G Gadget hackwrench

Source: Internet
Author: User

Topic Portal

Test instructions: A tree with a direction, asking if you can reach V

Analysis: Assume that there is no direction to the tree, DFS when the right value of +1, the inverse of the weight-1, and then find the LCA after the judgment DEP array and D array can be

`/************************************************* author:running_time* Created TIME:2015/10/5 Monday 10:28:49* Fil E Name:G_2.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e5 + 10;const int INF = 0x3f3f3f3f;const int D = 20;const int MOD = 1e9 + 7;const Dou ble EPS = 1e-8;struct Edge {int V, d, nex;}    Edge[n<<1];int Rt[d][n];int Dep[n], D[n];int head[n], e;void init (void) {memset (head,-1, sizeof (head)); e = 0;} void Add_edge (int u, int v, intdir) {Edge[e] = (edge) {V, dir, Head[u]}; Head[u] = e++;}    void DFS (int u, int fa, int dis) {Dep[u] = Dep[fa] + 1;    D[u] = dis;        for (int i=head[u]; ~i; i=edge[i].nex) {int v = EDGE[I].V;        if (v = = FA) continue;        RT[0][V] = u;    DFS (V, u, dis + edge[i].d);    }}int LCA (int u, int v) {if (Dep[u] < dep[v]) {swap (U, v);        } for (int i=0; i<d; ++i) {if ((Dep[u]-dep[v]) >> I & 1) {u = rt[i][u];    }} if (U = = v) return u;            for (int i=d-1; i>=0;-i) {if (Rt[i][u]! = Rt[i][v]) {u = rt[i][u];        v = rt[i][v]; }} return rt[0][v];}    int main (void) {int n;        while (scanf ("%d", &n) = = 1) {init ();            for (int u, V, i=1; i<n; ++i) {scanf ("%d%d", &u, &v);            Add_edge (U, V, 1);        Add_edge (V, U,-1);        } DFS (1, 0, 0);   for (int i=1; i<d; ++i) {         for (int j=1; j<=n; ++j) {rt[i][j] = rt[i-1][rt[i-1][j]];  }} int m;        scanf ("%d", &m);   while (m--) {int u, v;            scanf ("%d%d", &u, &v);            int f = LCA (U, v);            if (Dep[u]-dep[f]! = d[f]-d[u]) puts ("No");            else if (Dep[v]-dep[f]! = D[v]-d[f]) puts ("No");        Else puts ("Yes"); }} return 0;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.