AIZU Lowest Common Ancestor Tarjan求LCA
来源:互联网 发布:nginx ip hash 编辑:程序博客网 时间:2024/06/16 14:22
今天刚刚会写Tarjan离线LCA…(捂脸…)
代码如下:
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<vector>//by NeighThornusing namespace std;const int maxn=100000+5;int n,q,hd[maxn],to[maxn*2],nxt[maxn*2],cnt,vis[maxn],fa[maxn],ans[maxn];struct M{ int y,id; M(int a=0,int b=0){ y=a,id=b; }};vector<M> v[maxn];inline int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]); }inline void add(int x,int y){ to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++; }inline void tarjan(int root){ vis[root]=1; for(int i=0;i<(int)v[root].size();i++) if(vis[v[root][i].y]) ans[v[root][i].id]=find(v[root][i].y); for(int i=hd[root];i!=-1;i=nxt[i]) if(!vis[to[i]]) tarjan(to[i]),fa[to[i]]=root;}signed main(void){ scanf("%d",&n);cnt=0; memset(hd,-1,sizeof(hd)); for(int i=0,k;i<n;i++){ scanf("%d",&k); for(int j=1,x;j<=k;j++) scanf("%d",&x),add(i,x),add(x,i); }scanf("%d",&q); for(int i=0;i<n;i++) fa[i]=i,vis[i]=0; for(int i=1,x,y;i<=q;i++) scanf("%d%d",&x,&y),v[x].push_back(M(y,i)),v[y].push_back(M(x,i)); tarjan(0); for(int i=1;i<=q;i++) printf("%d\n",ans[i]); return 0; }
by >_< NeighThorn
1 0
- AIZU Lowest Common Ancestor Tarjan求LCA
- Lowest Common Ancestor(LCA)
- SPOJ LCA Lowest Common Ancestor
- LCA(lowest common ancestor)问题
- 最近公共祖先 LCA (Lowest Common Ancestor)
- LCA(lowest common ancestor)问题
- 剑指offer4-(LCA)Lowest Common Ancestor
- Range Minimum Query(RMQ) and Lowest Common Ancestor(LCA)
- Lowest Common Ancestor (LCA) of two nodes in graph
- LeetCode 236 Lowest Common Ancestor of a Binary Tree (LCA)
- 最近公共祖先LCA(Lowest Common Ancestor)
- POJ 1470 Cloest Common Ancestor(用Tarjan查询LCA)
- codechef Lowest Common Ancestor
- Lowest Common Ancestor
- Lowest Common Ancestor
- Lowest common ancestor
- [刷题]Lowest Common Ancestor
- Lowest/first common ancestor
- poj 1017
- Lua弱表
- JEM代码分析(一)CommonDef.h
- maven教程2-maven入门
- linux系统安装mycat,并配置读写分离
- AIZU Lowest Common Ancestor Tarjan求LCA
- 自定义控件(27)---自定义控件之组合控件(2) 通用的类似设置界面的样子
- iframe子页面调用父页面方法 跨域 异常 Blocked a frame with origin
- 如何基于报表工具FineReport进行二次开发
- XMLHttpRequest 传递中文 乱码
- 跑jar工程命令:
- 【NOIp 2003】【树结构·搜索】传染病防治
- Kendo UI头像上传 和 增删改查
- java 常用工具类 合集