[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- ...
随机推荐
- 在pytorch下使用tensorboardX(win10;谷歌浏览器;jupyter notebook)
使用环境:win10 ,在jupyter notebook下运行 谷歌浏览器 1.环境安装 使用conda 安装,打开anacond powershell,输入pip install tensorbo ...
- 代理模式是什么?如何在 C# 中实现代理模式
代理模式 并不是日常开发工作中常常用到的一种设计模式,也是一种不易被理解的一种设计模式.但是它会广泛的应用在系统框架.业务框架中. 定义 它的 定义 就如其它同大部分 设计模式 的定义类似,即不通俗也 ...
- 数据结构(C语言版)---二叉树
1.二叉树:任意一个结点的子结点个数最多两个,且子结点的位置不可更改,二叉树的子树有左右之分. 1)分类:(1)一般二叉树(2)满二叉树:在不增加树的层数的前提下,无法再多添加一个结点的二叉树就是满二 ...
- redis: 主从复制和哨兵模式(十三)
redis 主从复制 最低要求是一主二从(一个主机和两个从机) 主机才能写 从机只能读 只要从机连接到主机 数据就会全量复制到从机 环境配置(同一台机器) 1:配置文件 redis.conf配置如下: ...
- C#集合ArrayList、泛型集合List(3)
数组的制约:局限性.有多少放多少,要想追加,就必须重新再定义一个数组,这就造成了资源的极大浪费而且性能消耗也比较大.因此此操作不太推荐.所以集合就来了. ,,,} 创建集合: ArrayList li ...
- jquery选择时分插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- LeetCode 面试题56 - I. 数组中数字出现的次数 | Python
面试题56 - I. 数组中数字出现的次数 题目 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). ...
- token认证和理解
认知篇:https://blog.csdn.net/FYGu18/article/details/89345490 token失效篇认知:https://segmentfault.com/q/1010 ...
- 解释BOM头和去掉方法
http://www.thinkphp.cn/topic/2592.html 以上是叫你去掉bom头的,因为有些文件加载不出来就是window会以记事本的形式打开,然后默认给我们加了了bom头,有些文 ...
- python学习10字典
'''''''''字典(Dict)是python语言的一个最大的特征1.定义:是可变的无序集合,以键值对为基本元素,可以存储各种数据类型2.表示:{} 列表:[] 元组 () 字符串 ‘’ “” ‘‘ ...