1. 程式人生 > >tarjan--lca

tarjan--lca

自己寫的一個 模板,可以拿來參考一下

至於思路嗎

大概就是dfs每一個節點

到一個節點的時候就打上標記,然後訪問每個與這個節點有關的詢問,對於每個詢問,如果另一個節點是有標記的,則兩點的最近公共祖先就是另一個節點的祖先

然後再遍歷當前節點的每條邊,dfs邊的另一點,結束後再將另一點的祖先設定為當前點。<-這個比較重要,一定要是dfs之後再更換祖先

還有就是並查集的初始化一定不能忘記

#include<iostream>
#include<algorithm>
#define MAXN 500001
using namespace std;
int n,m,s;
struct line
{
int to;
int next;
}edge[MAXN*2];
struct query
{
int to;
int next;
int id;
}ques[MAXN*2];
int edgetot;
int quetot;
bool vis[MAXN];
int ans[MAXN];
int g[MAXN];
int qg[MAXN];
int pre[MAXN];
void addedge(int a,int b)
{
edgetot++;
edge[edgetot].to=b;
edge[edgetot].next=g[a];
g[a]=edgetot;
}
void addque(int a,int b,int i)
{
quetot++;
ques[quetot].to=b;
ques[quetot].id=i;
ques[quetot].next=qg[a];
qg[a]=quetot;
}
int find(int x)
{
if(x==pre[x])return x;
return pre[x]=find(pre[x]);
}
void dfs(int x)
{

vis[x]=true;
for(int i=qg[x];i;i=ques[i].next)
{
if(vis[ques[i].to]){ans[ques[i].id]=find(ques[i].to);}
}
for(int i=g[x];i;i=edge[i].next)
{
if(!vis[edge[i].to]){
dfs(edge[i].to);

pre[edge[i].to]=x;
}
}
return;
}
void init()
{
for(int i=1;i<=n;i++){
pre[i]=i;
vis[i]=false;
}
}
int main()
{
//std::ios::sync_with_stdio(false);
cin>>n>>m>>s;
for(int i=1;i<n;i++)
{
int x=0,y=0;
cin>>x>>y;
addedge(x,y);
addedge(y,x);
}
for(int i=0;i<m;i++)
{
int x=0,y=0;
cin>>x>>y;
addque(x,y,i);
addque(y,x,i);
}
init();
dfs(s);
for(int i=0;i<m;i++)
{
cout<<ans[i]<<endl;
}
return 0;
}

相關推薦

tarjan LCA模板

efi int namespace urn span void scanf 模板 ++ 1 #include<cstdio> 2 #include<iostream> 3 #define MN 300000 4 using namespac

tarjan--lca

自己寫的一個 模板,可以拿來參考一下 至於思路嗎 大概就是dfs每一個節點 到一個節點的時候就打上標記,然後訪問每個與這個節點有關的詢問,對於每個詢問,如果另一個節點是有標記的,則兩點的最近公共祖先就是另一個節點的祖先 然後再遍歷當前節點的每條邊,dfs邊的另一點,結束後再

poj 3694 Network(Tarjan+LCA

A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are con

POJ 3694 Tarjan+LCA

一個無向圖 求每增加一條邊後 圖的割邊的個數 #include<algorithm> #include<cstring> #include<cstdio> #include<vector> using namespace st

POJ1986 Distance Queries【最近公共祖先】【Tarjan-LCA演算法】

Distance Queries Time Limit: 2000MSMemory Limit: 30000K Total Submissions: 9777Accepted: 3425 Case Time Limit: 1000MS Description Farme

Tarjan&LCA題集【夏天的風】

【HDU】 [強連通]: 1269  迷宮城堡 判斷是否是一個強連通★ 2767Proving Equivalences  至少加幾條邊讓整個圖變成強連通★ 3836 Equivalent Sets  至少加幾條邊讓整個圖變成強連通★ 1827    Summer Holiday  傳遞的最小費用★★ 30

POJ3694-Network(Tarjan縮點+LCA)

urn amp con while utility lca memset include stream 題目鏈接 題意:給你一個連通圖。然後再給你n個詢問,每一個詢問給一個點u,v表示加上u,v之後又多少個橋。 思路:用Tarjan縮點後,形成一棵樹,所以樹邊

(轉)Tarjan應用:求割點/橋/縮點/強連通分量/雙連通分量/LCA(最近公共祖先)

應用 說明 lca ref father 無向圖 沒有 經理 遠的 本文轉載自:http://hi.baidu.com/lydrainbowcat/item/f8a5ac223e092b52c28d591c 作者提示:在閱讀本文之前,請確保您已經理解並掌握了基本的T

Tarjan】【LCA】【動態規劃】【推導】hdu6065 RXD, tree and sequence

and main ack find turn hdu mes ear 高明 劃分出來的每個區間的答案,其實就是連續兩個的lca的最小值。 即5 2 3 4 這個區間的答案是min(dep(lca(5,2)),dep(lca(2,3),dep(lca(3,4))))。 於是d

LCA離線算法Tarjan詳解

lca class 初始化 連通 一個 ans 為什麽 原理 子節點 離線算法也就是需要先把所有查詢給保存下來,最後一次輸出結果。 離線算法是基於並查集實現的,首先就是初始化P[i] = i。 接下來對於每個點進行dfs: ①首先判斷是否有與該點有關的查詢,如果當前該

LCA-tarjan understand 2

pid strong edge lca arc repl root memset html 下面是一個最基礎的LCA題目 http://poj.org/problem?id=1330 赤裸裸的 題意 輸入cas 後 有cas組數據 輸入 n 再輸入n-1 條邊

Tarjan之求LCA

算法 get ios nbsp read 而在 hid 統一 turn Tarjan之求LCA 不要問我為什麽寫完Tarjan還要再補一句“求LCA的那個” 因為只說Tarjan的話完全不知道你指的是哪個算法…… 勞模Tarjan同誌證明了好多算法,而且全都叫Tarjan算

【C++】最近公共祖先LCATarjan離線算法)&& 洛谷P3379LCA模板

target sizeof add 例題 開始 實現 再看 根節點 strong 1.前言   首先我們介紹的算法是LCA問題中的離線算法-Tarjan算法,該算法采用DFS+並查集,再看此算法之前首先你得知道並查集(盡管我相信你如果知道這個的話肯定是知道並查集的),

筆記:LCA最近公共祖先 Tarjan(離線)算法

否則 二叉樹 tail [] info enter 父親節 自己 初始 LCA最近公共祖先 Tarjan他賤(離線)算法的基本思路及其算法實現 本文是網絡資料整理或部分轉載或部分原創,參考文章如下: https://www.cnblogs.com/JVxie/p/4

公共lcaTarjan

div gre col 方法 post color 公共祖先 com for 首先,我們先來了解LCA。 LCA 是樹上兩個點最近的公共祖先。 比如說,在如圖的樹中,3與4的公共祖先有“2”,“1”,但最近的祖先是“2”。 顯然,暴力可以做O(n),但是我們希望更快。 現

[差分][倍增lca][tarjan] Jzoj P3325 壓力

sizeof 多少 pri mat lds 解釋 必須 當前 const Description 如今,路由器和交換機構建起了互聯網的骨架。處在互聯網的骨幹位置的核心路由器典型的要處理100Gbit/s的網絡流量。他們每天都生活在巨大的壓力之下。小強建立了一個模型。這

tarjan,樹剖,倍增求lca

next 訪問 find int ext for pac using ins 1.tarjan求lca Tarjan(u)//marge和find為並查集合並函數和查找函數 { for each(u,v) //訪問所有u子節點v    {  

[hdu2460]network(依次連邊並詢問圖中割邊數量) tarjan邊雙聯通分量+lca

雙連通 16px std stream eof get 數據 new ace 題意: 給定一個n個點m條邊的無向圖,q個操作,每個操作給(x,y)連邊並詢問此時圖中的割邊有多少條。(連上的邊會一直存在) n<=1e5,m<=2*10^5,q<=1e3,多組

POJ 1986 Distance Queries 【輸入YY && LCATarjan離線)】

script ota const mem limit 分享 pre mea run 任意門:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 3000

POJ 1470 Closest Common Ancestors (模板題)(Tarjan離線)【LCA

clear pac push 公共祖先 back family ble lan tarjan <題目鏈接> 題目大意:給你一棵樹,然後進行q次詢問,然後要你統計這q次詢問中指定的兩個節點最近公共祖先出現的次數。 解題分析:LCA模板題,下面用的是離線Tarjan