博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[csu/coj 1079]树上路径查询 LCA
阅读量:5167 次
发布时间:2019-06-13

本文共 4951 字,大约阅读时间需要 16 分钟。

题意:询问树上从u到v的路径是否经过k

思路:把树dfs转化为有根树后,对于u,v的路径而言,设p为u,v的最近公共祖先,u到v的路径必定是可以看成两条路径的组合,u->p,v->p,这样一来便可以将判断条件转化为(LCA(u,k)=k  || LCA(v,k)=k) && LCA(k,p)=p。由于这个LCA询问里面需要用到中间结果p,所以这种方法用tarjan离线不行,只能用dfs+RMQ。

1 #pragma comment(linker, "/STACK:10240000,10240000")  2   3 #include 
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 23 using namespace std; 24 25 #define mem0(a) memset(a, 0, sizeof(a)) 26 #define mem_1(a) memset(a, -1, sizeof(a)) 27 #define lson l, m, rt << 1 28 #define rson m + 1, r, rt << 1 | 1 29 #define define_m int m = (l + r) >> 1 30 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 31 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 32 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 33 #define rep_down1(a, b) for (int a = b; a > 0; a--) 34 #define all(a) (a).begin(), (a).end() 35 #define lowbit(x) ((x) & (-(x))) 36 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 37 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 38 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 39 #define pchr(a) putchar(a) 40 #define pstr(a) printf("%s", a) 41 #define sstr(a) scanf("%s", a) 42 #define sint(a) scanf("%d", &a) 43 #define sint2(a, b) scanf("%d%d", &a, &b) 44 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c) 45 #define pint(a) printf("%d\n", a) 46 #define test_print1(a) cout << "var1 = " << a << endl 47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl 49 #define mp(a, b) make_pair(a, b) 50 #define pb(a) push_back(a) 51 52 typedef long long LL; 53 typedef pair
pii; 54 typedef vector
vi; 55 56 const int dx[8] = { 0, 0, -1, 1, 1, 1, -1, -1}; 57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 58 const int maxn = 4e5 + 7; 59 const int md = 1e9 + 7; 60 const int inf = 1e9 + 7; 61 const LL inf_L = 1e18 + 7; 62 const double pi = acos(-1.0); 63 const double eps = 1e-6; 64 65 template
T gcd(T a, T b){ return b==0?a:gcd(b,a%b);} 66 template
bool max_update(T &a,const T &b){ if(b>a){a = b; return true;}return false;} 67 template
bool min_update(T &a,const T &b){ if(b
T condition(bool f, T a, T b){ return f?a:b;} 69 template
void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 70 int make_id(int x, int y, int n) { return x * n + y; } 71 72 struct SparseTable { 73 int d[maxn][22]; 74 int t[maxn]; 75 void Init_min(int a[], int n) { 76 rep_up0(i, n) d[i][0] = a[i]; 77 for (int j = 1; (1 << j) <= n; j++) { 78 for (int i = 0; i + (1 << j) - 1 < n; i++) { 79 d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]); 80 } 81 } 82 int p = -1; 83 rep_up1(i, n) { 84 if ((i & (i - 1)) == 0) p++; 85 t[i] = p; 86 } 87 } 88 void Init_max(int a[], int n) { 89 rep_up0(i, n) d[i][0] = a[i]; 90 for (int j = 1; (1 << j) <= n; j++) { 91 for (int i = 0; i + (1 << j) - 1 < n; i++) { 92 d[i][j] = max(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]); 93 } 94 } 95 int p = -1; 96 rep_up1(i, n) { 97 if ((i & (i - 1) == 0)) p++; 98 t[i] = p; 99 }100 }101 int RMQ_min(int L, int R) {102 int p = t[R - L + 1];103 return min(d[L][p], d[R - (1 << p) + 1][p]);104 }105 int RMQ_max(int L, int R) {106 int p = t[R - L + 1];107 return max(d[L][p], d[R - (1 << p) + 1][p]);108 }109 };110 111 SparseTable st;112 113 struct Graph {114 vector
> G;115 void clear() { G.clear(); }116 void resize(int n) { G.resize(n + 2); }117 void add(int u, int v) { G[u].push_back(v); }118 vector
& operator [] (int u) { return G[u]; }119 };120 Graph G;121 122 int dfs_clock;123 int a[maxn], b[maxn], r[maxn];124 125 void dfs(int u, int fa) {126 r[u] = dfs_clock;127 a[dfs_clock ++] = u;128 int sz = G[u].size();129 rep_up0(i, sz) {130 int v = G[u][i];131 if (fa != v) {132 dfs(v, u);133 a[dfs_clock ++] = u;134 }135 }136 }137 138 int LCA(int u, int v) {139 if (r[u] > r[v]) swap(u, v);140 return a[st.RMQ_min(r[u], r[v])];141 }142 143 bool chk(int u, int v, int w) {144 int pos = LCA(u, v);145 return (LCA(u, w) == w || LCA(v, w) == w) && LCA(pos, w) == pos;146 }147 148 int main() {149 //freopen("in.txt", "r", stdin);150 int n, q;151 while (cin >> n >> q) {152 G.clear();153 G.resize(n);154 rep_up0(i, n - 1) {155 int u, v;156 sint2(u, v);157 G.add(u, v);158 G.add(v, u);159 }160 dfs_clock = 0;161 dfs(1, 0);162 rep_up0(i, dfs_clock) b[i] = r[a[i]];163 st.Init_min(b, dfs_clock);164 rep_up0(i, q) {165 int u, v, w;166 sint3(u, v, w);167 puts(chk(u, v, w)? "YES" : "NO");168 }169 cout << endl;170 }171 return 0;172 }
View Code

 

转载于:https://www.cnblogs.com/jklongint/p/4509077.html

你可能感兴趣的文章
关于PHP会话:session和cookie
查看>>
LintCode-Backpack
查看>>
查询数据库锁
查看>>
面试时被问到的问题
查看>>
注解小结
查看>>
201421410014蒋佳奇
查看>>
Xcode5和ObjC新特性
查看>>
CSS属性值currentColor
查看>>
Real-Time Rendering 笔记
查看>>
多路复用
查看>>
【UVA】434-Matty&#39;s Blocks
查看>>
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
使用Reporting Services时遇到的小问题
查看>>
约瑟夫问题
查看>>
Arduino 报错总结
查看>>
树莓派Android Things物联网开发:树莓派GPIO引脚图
查看>>
矩阵快速幂---BestCoder Round#8 1002
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
awk变量
查看>>
mysql_对于DQL 的简单举例
查看>>