[csu/coj 1079]树上路径查询 LCA
题意:询问树上从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。
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <stack>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#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) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 4e5 + ;
const int md = 1e9 + ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } struct SparseTable {
int d[maxn][];
int t[maxn];
void Init_min(int a[], int n) {
rep_up0(i, n) d[i][] = a[i];
for (int j = ; ( << j) <= n; j++) {
for (int i = ; i + ( << j) - < n; i++) {
d[i][j] = min(d[i][j - ], d[i + ( << (j - ))][j - ]);
}
}
int p = -;
rep_up1(i, n) {
if ((i & (i - )) == ) p++;
t[i] = p;
}
}
void Init_max(int a[], int n) {
rep_up0(i, n) d[i][] = a[i];
for (int j = ; ( << j) <= n; j++) {
for (int i = ; i + ( << j) - < n; i++) {
d[i][j] = max(d[i][j - ], d[i + ( << (j - ))][j - ]);
}
}
int p = -;
rep_up1(i, n) {
if ((i & (i - ) == )) p++;
t[i] = p;
}
}
int RMQ_min(int L, int R) {
int p = t[R - L + ];
return min(d[L][p], d[R - ( << p) + ][p]);
}
int RMQ_max(int L, int R) {
int p = t[R - L + ];
return max(d[L][p], d[R - ( << p) + ][p]);
}
}; SparseTable st; struct Graph {
vector<vector<int> > G;
void clear() { G.clear(); }
void resize(int n) { G.resize(n + ); }
void add(int u, int v) { G[u].push_back(v); }
vector<int> & operator [] (int u) { return G[u]; }
};
Graph G; int dfs_clock;
int a[maxn], b[maxn], r[maxn]; void dfs(int u, int fa) {
r[u] = dfs_clock;
a[dfs_clock ++] = u;
int sz = G[u].size();
rep_up0(i, sz) {
int v = G[u][i];
if (fa != v) {
dfs(v, u);
a[dfs_clock ++] = u;
}
}
} int LCA(int u, int v) {
if (r[u] > r[v]) swap(u, v);
return a[st.RMQ_min(r[u], r[v])];
} bool chk(int u, int v, int w) {
int pos = LCA(u, v);
return (LCA(u, w) == w || LCA(v, w) == w) && LCA(pos, w) == pos;
} int main() {
//freopen("in.txt", "r", stdin);
int n, q;
while (cin >> n >> q) {
G.clear();
G.resize(n);
rep_up0(i, n - ) {
int u, v;
sint2(u, v);
G.add(u, v);
G.add(v, u);
}
dfs_clock = ;
dfs(, );
rep_up0(i, dfs_clock) b[i] = r[a[i]];
st.Init_min(b, dfs_clock);
rep_up0(i, q) {
int u, v, w;
sint3(u, v, w);
puts(chk(u, v, w)? "YES" : "NO");
}
cout << endl;
}
return ;
}
[csu/coj 1079]树上路径查询 LCA的更多相关文章
- 【CSU 1079】树上的查询
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1079 现有一棵有N个顶点的树,顶点的标号分别为1, 2, …, N.对于每个形如a b k的询问, ...
- LCA+主席树 (求树上路径点权第k大)
SPOJ 10628. Count on a tree (树上第k大,LCA+主席树) 10628. Count on a tree Problem code: COT You are given ...
- hihocoder[Offer收割]编程练习赛19 D 相交的铁路线(树上路径交)
傻逼题... 裸的树上路径交 两条树上的路径$[a,b]$和$[c,d]$有交,则有$lca(a,b)$在$[c,d]$上或$lca(c,d)$在$[a,b]$上. 其实只要深度大的$lca$在另一条 ...
- 树上路径(path)
树上路径(path) 题目描述 在Berland,有n个城堡. 每个城堡恰好属于一个领主.不同的城堡属于不同的领主.在所有领主中有一个是国王,其他的每个领主都直接隶属于另一位领主,并且间接隶属于国王. ...
- CentOS7通过 yum安装路径查询方法
CentOS7通过 yum安装路径查询方法 rpm -qa 然后执行 rpm -ql 软件名称 就可以显示软件的安装路径. 原文博客的链接地址:https://cnblogs.com/qzf/
- 【BZOJ3784】树上路径
题目大意 给定一个\(N\)个结点的树,结点用正整数\(1..N\)编号.每条边有一个正整数权值.用\(d(a,b)\)表示从结点\(a\)到结点\(b\)路边上经过边的权值.其中要求\(a < ...
- P3398 仓鼠找sugar 树上路径相交判断
\(\color{#0066ff}{题目描述}\) 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐 ...
- 某模拟赛C题 树上路径统计 (点分治)
题意 给定一棵有n个节点的无根树,树上的每个点有一个非负整数点权.定义一条路径的价值为路径上的点权和-路径上的点权最大值. 给定参数P,我!=们想知道,有多少不同的树上简单路径,满足它的价值恰好是P的 ...
- 【JZOJ4715】【NOIP2016提高A组模拟8.19】树上路径
题目描述 给出一棵树,求出最小的k,使得,且在树中存在路径p,使得k>=S且k<=E.(k为路径p上的边的权值和) 输入 第一行给出N,S,E.N代表树的点数,S,E如题目描述. 下面N- ...
随机推荐
- vue2.x学习笔记(十)
接着前面的内容:https://www.cnblogs.com/yanggb/p/12584237.html. 事件处理 使用javascript当然少不了事件处理,即使是vue也不会例外. 监听事件 ...
- [Abp vNext 入坑分享] - 3.简单的用户模块功能开发
一.简要说明 本篇文章开始进行业务模块的开发模拟,借助user模块来进行业务开发,主要是用户相关的基础操作.主要是先使用Users来体验整个开发的流程.主要是先把一个基础流程跑顺利,在这里我并不会过于 ...
- 手机照片的exif里有方向属性
<?php $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name'])); $ex ...
- MySQL join的7种理论及SQL写法
转载于 https://www.cnblogs.com/dinglinyong/p/6656315.html 建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE d ...
- FluentAspects -- 基于 Fluent API 的 Aop
FluentAspects -- 基于 Fluent API 的 Aop Intro 上次我们做了一个简单的 AOP 实现示例,但是实现起来主要是基于 Attribute 来做的,对于代码的侵入性太强 ...
- 云时代 • 新契机:2017届中国SaaS产业大会圆满落幕
2017年5-6日,由拓普会展携手中国云体系产业创新战略联盟主办,江苏省企业信息化协会,浙江省企业信息化促进会,广东省首席信息官协会,CIO时代学院,IDC点评网协办以及上海市网购商会,中国信息化推进 ...
- JavaScript 后台获取数据 - HTTP203 Advent(中文字幕)
如果关注过 Google 相关的开发技术,对 HTTP203 这个栏目应该不陌生. 这是 HTTP203 圣诞节的特别版! Jake(@jaffathecake)和 Surma(@DasSurma)有 ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
- 10.秋招复习简单整理之CSMA/CD协议
我们知道,总线上只要有一台计算机在发送数据,总线的传输资源就被占用.因此,在同一时间只能允许一台计算机发送数据,否则各计算机之间就会相互干扰,使得所发送的数据被破坏.因此,如何协调总线上各计算机的工作 ...