题目传送门

题意:一棵有向的树,问u到v是否可达

分析:假设是无向树,DFS时正向的权值+1,反向的权值-1,然后找到LCA后判断dep数组和d数组就可以了

/************************************************
* Author :Running_Time
* Created Time :2015/10/5 星期一 10:28:49
* File 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 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int D = 20;
const int MOD = 1e9 + 7;
const double 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, int dir) {
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;
}

  

LCA Codeforces 100685G Gadget Hackwrench的更多相关文章

  1. (Gym 100685G) Gadget Hackwrench(LCA在线ST)

    Gadget Hackwrench time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...

  2. Gym100685G Gadget Hackwrench(倍增LCA)

    题目大概说一棵边有方向的树,q个询问,每次询问结点u是否能走到v. 倍增LCA搞即可: 除了par[k][u]表示u结点往上走2k步到达的结点, 再加上upp[k][u]表示u结点往上走2k步经过边的 ...

  3. #map+LCA# Codeforces Round #362 (Div. 2)-C. Lorenzo Von Matterhorn

    2018-03-16 http://codeforces.com/problemset/problem/697/C C. Lorenzo Von Matterhorn time limit per t ...

  4. codeforces 519E A and B and Lecture Rooms LCA倍增

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  5. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  6. Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca

    题目链接: http://codeforces.com/contest/575/problem/B 题解: 把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的). ...

  7. Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)

    题目链接:http://codeforces.com/problemset/problem/697/D 给你一个有规则的二叉树,大概有1e18个点. 有两种操作:1操作是将u到v上的路径加上w,2操作 ...

  8. Codeforces 1000G Two-Paths 树形动态规划 LCA

    原文链接https://www.cnblogs.com/zhouzhendong/p/9246484.html 题目传送门 - Codeforces 1000G Two-Paths 题意 给定一棵有 ...

  9. Codeforces Round #520 (Div. 2) E. Company(dfs序判断v是否在u的子树里+lca+线段树)

    https://codeforces.com/contest/1062/problem/E 题意 给一颗树n,然后q个询问,询问编号l~r的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...

随机推荐

  1. HDU 6086 Rikka with String AC自动机 + DP

    Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...

  2. Codeforces Round #422 (Div. 2) D. My pretty girl Noora 数学

    D. My pretty girl Noora     In Pavlopolis University where Noora studies it was decided to hold beau ...

  3. strong and weak 强引用和弱引用的差别

    (weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会被释放  ,即使还有weak型指针指向它. 一旦最后一个strong型指针离去 .这个对象将被释放,全部剩余 ...

  4. td 中连续数字或连续英文内容不自动换行

    原因: 把连续的英文当做成了一个单词. 解决: 加上 : word-break: break-all (允许单词内换行)

  5. FMDB 使用技巧

    源链接:  http://blog.csdn.net/iunion/article/details/7091744 - (BOOL) isTableOK:(NSString *)tableName{  ...

  6. Oracle:imp导入imp-00000问题

    现场环境:window2008 . oracle11.2g  .客户端安装的是oracle10g一个简洁版 34M的. 在imp导入时,提示 Message 100 not found; No mes ...

  7. 【Selenium】显示、隐式等待

    显示等待 WebDriverWait 超时抛出TimeOutException,默认500毫秒 public class WaitToReturnElement { /* * 设置超时时间为5秒,返回 ...

  8. CGAffineTransform属性

    transform我们一般称为形变属性,其本质是通过矩阵变化改变控件的大小.位置.角度等,这里我们通过一个例子来看一下具体的操作,在下面的例子中我们也会看到UIImageView控件的常用操作. - ...

  9. Android「后台下载」Feb.24小记

    参考了CSDN上的这个文章(HERE),之前只是新开一个线程: public class DownloadThread implements Runnable{ String tarFile ; pu ...

  10. Top的VIRT是什么

    Top命令监控某个进程的资源占有情况  下面是各种内存: VIRT:virtual memory usage 1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等     2.假如进程申请1 ...