首页 > 其他分享 >RMQ求LCA

RMQ求LCA

时间:2022-09-28 11:56:30浏览次数:37  
标签:outp RMQ outbuf int inbuf LCA inp1 SIZE

RMQ求LCA:使用欧拉序

点击查看代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
const int IN_SIZE = 200005, OUT_SIZE = 200005;
char inbuf[IN_SIZE + 5], *inp1 = inbuf, *inp2 = inbuf, outbuf[OUT_SIZE + 5], *outp = outbuf;
#define getchar() (inp1 == inp2 && (inp2 = (inp1 = inbuf) + fread(inbuf, 1, IN_SIZE, stdin), inp1 == inp2) ? EOF : *inp1 ++)
#define putchar(c) (*outp ++ = c, (outp - outbuf == OUT_SIZE) && (fwrite(outbuf, outp - outbuf, 1, stdout), outp = outbuf))
#define flush() (fwrite(outbuf, outp - outbuf, 1, stdout), 0)
inline int read() {
	int x = 0, c = getchar();
	while(!isdigit(c)) c = getchar();
	while(isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
	return x;
}
inline void write(int x) {
	static char dig[20];
	static int tmp, t;
	t = 0;
	do tmp = x / 10, dig[++ t] = x - (tmp << 1) - (tmp << 3), x = tmp;
	while(x);
	for(int i = t; i; i --) putchar(dig[i] ^ 48);
}
inline int max(int x, int y) { return x > y ? x : y; }
inline int min(int x, int y) { return x < y ? x : y; }

const int N = 2e5 + 5, M = N << 1, logN = 25;
int n, m, rt, h[N], e[M], nxt[M], idx;
int st[logN][M], dfn[M], tms, id[N], lg[M];
void add(int a, int b) { e[++ idx] = b, nxt[idx] = h[a], h[a] = idx; }
void dfs(int u, int fa) {
	dfn[++ tms] = u, id[u] = tms;
	for(int i = h[u]; i; i = nxt[i]) {
		int v = e[i];
		if(v != fa) dfs(v, u), dfn[++ tms] = u;
	}
}
int LCA(int x, int y) {
	if(x == y) return x;
	int l = min(id[x], id[y]), r = max(id[x], id[y]), t = lg[r - l + 1];
	return dfn[min(st[t][l], st[t][r - (1 << t) + 1])];
}
int main() {
	n = read(), m = read(), rt = read();
	for(int i = 1, a, b; i < n; i ++) a = read(), b = read(), add(a, b), add(b, a);
	dfs(rt, 0);
	lg[0] = -1; for(int i = 1; i <= tms; i ++) lg[i] = lg[i >> 1] + 1, st[0][i] = id[dfn[i]];
	for(int j = 1; j < logN; j ++)
		for(int i = 1; i + (1 << j) - 1 <= tms; i ++)
			st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
	for(int i = 1, a, b, c = 0; i <= m; i ++)
		a = (read() + c) % n + 1, b = (read() + c) % n + 1, write(c = LCA(a, b)), putchar(10);
	return flush();
}

标签:outp,RMQ,outbuf,int,inbuf,LCA,inp1,SIZE
From: https://www.cnblogs.com/azzc/p/16737492.html

相关文章

  • LCA(最近公共祖先)求解方法
    本文参考https://oi-wiki.org/graph/lca/定义树上u,v两点的LCA(最近公共祖先)是从根节点dfs到上述两节点路径上距离上述两点最近的公共点。LCA有如下性质:1、u是v的祖先,当且......
  • 6th 2022/6/8 算法总结·LCA·倍增
    开头的话这个算法对于大部分图论无环图题都是必备的,应多多复习大概是对于两个点的公共祖先倍增众所周知,为了找到公共祖先,最暴力的算法就莫过于一个个往上跳,直至相遇而......
  • 12th 2022/7/11 RMQ专题复习
    分为三类吧线段树这种数据结构挺有用的,使用范围是时间,看着办嘛,\(O(n\logn)\)的算法,修改加入查询都是\(O(\logn)\)然后建树\(O(n\logn)\)看着办大概思路就是将一个......
  • LCA&DSU&MST
    目录$\text{LCA}$时间复杂度树上差分最小瓶颈路树上路径相交无根树$\text{LCA}$小结$\text{DSU}$时间复杂度带权并查集扩展域连通时间戳断边时间戳小结\(\text{M......
  • FullCalendar日程管理控件(二)
    1css#calendar{max-width:1100px;margin:20pxauto;}.fc-license-message{display:none;}......
  • And RMQ 势能线段树-用剪枝+单点修改实现区间修改
    https://codeforces.com/gym/103107/problem/AA.AndRMQtimelimitpertest3secondsmemorylimitpertest512megabytesinputstandardinputoutputstan......
  • Webrtc Video Simulcast
    titleWebRTCVideoSimulcastClient->PeerConnection:SetLocalDescriptionPeerConnection->SdpOfferAnswerHandler:SetLocalDescriptionSdpOfferAnswerHandler->SdpOffer......
  • LCA
    定义LCA(LeastCommonAncestors),即最近公共祖先,指对于有根树TT的两个结点uu、vv,最近公共祖先LCA(T,u,v)LCA(T,u,v)表示一个结点xx,满足xx是uu、vv的祖先且xx......
  • FullCalendar日程管理控件
    1官网https://fullcalendar.io/2参考文档https://www.cnblogs.com/cnsyear/p/13915215.html   注:参考文档的版本可能有问题,尽量使用官网中的英文文档,相对比较准......
  • J [NOIP2013]货车运输 lca 最大生成树 点和点之间所有路径最小值的最大值
     链接:https://ac.nowcoder.com/acm/problem/16527来源:牛客网题目描述A国有n座城市,编号从1到n,城市之间有m条双向道路。每一条道路对车......