luogu3379 最近公共祖先(LCA) tarjan 倍增
来源:互联网 发布:数据采集技术有哪些 编辑:程序博客网 时间:2024/06/07 05:33
读入优化使OJ快乐!
tarjan不多言
#include <iostream>#include <cstdio>using namespace std;struct Edge{ int too, nxt;}edge[1000005];struct Ques{ int nxt, too, ran;}ques[1000005];int getint(){ char ch=getchar(); int re=0; while(ch<'0' || ch>'9') ch = getchar(); while(ch>='0' && ch<='9'){ re = re*10 + ch - '0'; ch = getchar(); } return re;}int n, m, ecnt, qcnt, ehea[500005], qhea[500005], fa[500005], uu, vv;int s, ans[500005];bool vis[500005];void add_edge(int fro, int too){ edge[++ecnt].nxt = ehea[fro]; edge[ecnt].too = too; ehea[fro] = ecnt;}void add_ques(int fro, int too, int ran){ ques[++qcnt].nxt = qhea[fro]; ques[qcnt].too = too; ques[qcnt].ran = ran; qhea[fro] = qcnt;}int myfind(int x){ return fa[x]==x?x:fa[x]=myfind(fa[x]);}void tarjan(int u){ vis[u] = true; for(int i=ehea[u]; i; i=edge[i].nxt){ int t=edge[i].too; if(!vis[t]){ tarjan(t); fa[t] = u; } } for(int i=qhea[u]; i; i=ques[i].nxt){ int t=ques[i].too; if(vis[t]) ans[ques[i].ran] = myfind(t); }}int main(){ n = getint(); m = getint(); s = getint(); for(int i=1; i<n; i++){ uu = getint(); vv = getint(); add_edge(uu, vv); add_edge(vv, uu); fa[i] = i; } fa[n] = n; for(int i=1; i<=m; i++){ uu = getint(); vv = getint(); add_ques(uu, vv, i); add_ques(vv, uu, i); } tarjan(s); for(int i=1; i<=m; i++) printf("%d\n", ans[i]); return 0;}
倍增亦不多言
#include <iostream>#include <cstdio>using namespace std;int n, m, s, grand[500005][22], deep[500005], hea[500005], cnt, uu, vv;struct Edge{ int too, nxt;}edge[1000005];int getint(){ char ch=getchar(); int re=0; while(ch<'0' || ch>'9') ch = getchar(); while(ch>='0' && ch<='9'){ re = re*10 + ch - '0'; ch = getchar(); } return re;}void add_edge(int fro, int too){ edge[++cnt].nxt = hea[fro]; edge[cnt].too = too; hea[fro] = cnt;}void builddepth(int u){ for(int i=hea[u]; i; i=edge[i].nxt){ int t=edge[i].too; if(!deep[t]){ deep[t] = deep[u] + 1; grand[t][0] = u; builddepth(t); } }}void init(){ deep[s] = 1; builddepth(s); for(int i=1; i<=19; i++) for(int j=1; j<=n; j++) grand[j][i] = grand[grand[j][i-1]][i-1];}int getlca(int x, int y){ if(deep[x]<deep[y]){ x ^= y; y ^= x; x ^= y; } for(int i=19; i>=0; i--) if(deep[grand[x][i]]>=deep[y]) x = grand[x][i]; if(x==y) return x; for(int i=19; i>=0; i--) if(grand[x][i]!=grand[y][i]){ x = grand[x][i]; y = grand[y][i]; } return grand[x][0];}int main(){ n = getint(); m = getint(); s = getint(); for(int i=1; i<n; i++){ uu = getint(); vv = getint(); add_edge(uu, vv); add_edge(vv, uu); } init(); for(int i=1; i<=m; i++){ uu = getint(); vv = getint(); printf("%d\n", getlca(uu, vv)); } return 0;}
阅读全文
0 0
- luogu3379 最近公共祖先(LCA) tarjan 倍增
- 最近公共祖先 LCA 倍增+Tarjan实现
- 最近公共祖先(LCA):tarjan与倍增
- c++最近公共祖先LCA(倍增算法和tarjan)
- 最近公共祖先LCA(Tarjan与DFS--ST倍增)
- 最近公共祖先(LCA)---倍增法
- 最近公共祖先(LCA):倍增
- LCA最近公共祖先(RMQ、Tarjan)
- [算法] LCA 最近公共祖先 (Tarjan)
- 最近公共祖先(LCA)算法实现过程 【Tarjan离线+倍增在线+RMQ】
- 最近公共祖先LCA tarjan
- 【讲解+模板】最近公共祖先(LCA)(倍增)
- LCA最近公共祖先(朴素+倍增法)
- 树上倍增求LCA(最近公共祖先)
- lca(最近公共祖先)倍增模板【pascal】
- LCA(最近公共祖先)倍增法模板及总结
- lca最近公共祖先(st表/倍增)
- LCA(最近公共祖先)倍增法实现
- Django-REST之1:serializers序列器-翻译及自己的学习笔记(更新完善中...)
- 二叉树的建立
- Mac下 常用程序 无限试用方法
- A*寻路极限优化
- spring boot API 完整版
- luogu3379 最近公共祖先(LCA) tarjan 倍增
- [POJ3259]Wormholes(负环)
- Django基础,Day8
- HDU 6113 度度熊的01世界(dfs)
- AI时代职场人何去何从?五大葩王带你深入解读
- 雾霾压境,你真的选对了空气净化器吗?
- 百度云的使命:AI赋能企业的快速通道
- 进击的雷神科技:新三板首支“游戏生态”概念股,掀新国货浪潮
- 高德打造全民出行节 十一出游“心不堵”