tarjan LCA模板

 1 #include<cstdio>
 2 #include<iostream>
 3 #define MN 300000
 4 using namespace std;
 5 int n,m,w[MN],cnt,h[MN],q[MN];
 6 int s[MN],t[MN],fa[MN],dis[MN],a[MN];
 7 bool vis[MN];
 8 struct edge{int to,next;}e[MN*8];
 9 void ins(int *h,int u,int v){e[++cnt].to=v;e[cnt].next=h[u];h[u]=cnt;}
10 int ff(int x)={return fa[x]?fa[x]ff(fa[x]):x;}
11 void tarjan(int u){
12     vis[u]=true;
13     for(int i=h[u];i;i=e[i].next){
14         int v=e[i].to;
15         if(!vis[v]){
16             dis[v]=dis[u]+1; tarjan(v); fa[v]=u;
17         }
18     }
19     for(int i=q[u];i;i=e[i].next){
20         int v=e[i].to;
21         if(a[v]) a[v]=ff(a[v]);
22         else a[v]=u;
23     }
24 }
25 int main()
26 {
27     scanf("%d%d",&n,&m);
28     for(int i=1;i<n;i++){
29         int u,v; scanf("%d%d",&u,&v); ins(h,u,v);
30     }
31     for(int i=1;i<=n;i++) scanf("%d",&w[i]);
32     for(int i=1;i<=m;i++){
33         scanf("%d%d",&s[i],&t[i]); ins(q,s[i],i); ins(q,t[i],i);
34     }
35     tarjan(1);
36 }
时间: 2024-10-11 05:47:18

tarjan LCA模板的相关文章

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo

POJ 1330 Nearest Common Ancestors(LCA模板)

给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果访问过的话,那么它的最近公共祖先就是当前节点祖先. 下面是tarjan离线模板: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn =

tarjan算法模板

var {left表示点 root 没离开栈 vis表示点 root 有没有被访问过} i,n,m,now,time,color,top:longint; v:array[0..10001] of record start:longint;end; e:array[0..100001] of record y,next:longint;end; dfn,low,stack,encolor:array[0..10001] of longint; vis,left:array[0..10001] o

HDU 5296 Annoying problem(LCA模板+树的dfs序心得)

Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,-,n, which has n-1 edge,each edge has a weight. An existing set S is initially empty. Now there are two kinds of operation: 1 x: If the node x is not in the set S, add n

算法复习——LCA模板(POJ1330)

题目: Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancesto

LCA模板整理

HDU2586 纯LCA模板 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define lowbit(x) x&(-x) #define rep(i,l,r) for(int i=l;i<=r;++i) #define per(i,r,l) for(int i=r;i>=l;--i) #define ls o<<1 #defin

LCA模板(数剖实现)

题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n),每次查询为logn,因此总复杂度为:O(2*n+m*logn). 代码: #include<cstdio> #include<cstring> using namespace std; const int maxn=500005; struct node{ int v,next; }

POJ 1330(LCA模板)

链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 #include<set> 6 #include<string> 7 #incl

HDU 2586 How far away ? (离线LCA Tarjan算法模板)

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6422    Accepted Submission(s): 2411 Problem Description There are n houses in the village and some bidirectional roads connecting