LCA法寶 並查集

2021-09-11 07:42:40 字數 490 閱讀 1857

由於我也是看懂的,突然發現這篇部落格特別的好!!

lca 最近公共祖先

**   

我相信你現在肯定自己就可以打**了!

另外注意,這裡的 f 陣列其實是用了並查集的思想

並查集詳解 ***

倍增**

inline void dfs(int x, int s, int chang)

for(reg int i = 0; i < g[x].size(); i++)

}}inline void change(int &x, int y)

}inline int beizeng(int x, int y)

if(shendu[y] > shendu[x])

if(x == y)

return x;

for(reg int i = 20; i >= 0; i--)

}return p[x];

}

3910 火車 LCA 並查集

在樹上走顯然是求lca,然後每次走完把端點到lca路徑上的點都用並查集合並,之後如果判斷兩點所屬集合相等說明已經走過。倍增被鏈剖虐成狗系列 include include define n 500005 define ll long long using namespace std int n,m,...

並查集 並查集

本文參考了 挑戰程式設計競賽 和jennica的github題解 陣列版 int parent max n int rank max n void init int n int find int x else void union int x,int y else 結構體版 struct node ...

bzoj3910 火車 lca 並查集

a 國有n 個城市,城市之間有一些雙向道路相連,並且城市兩兩之間有唯一 路徑。現在有火車在城市 a,需要經過m 個城市。火車按照以下規則行駛 每次 行駛到還沒有經過的城市中在 m 個城市中最靠前的。現在小 a 想知道火車經過 這m 個城市後所經過的道路數量。第一行三個整數 n m a,表示城市數量 ...