BZOJ4668 冷戰 並查集 LCA

2022-05-09 01:24:07 字數 581 閱讀 9024

題意:動態給點連邊 詢問兩個點之間最早是在第幾個操作連起來的

題解:因為並查集按秩合併 秩最高是logn的 所以我們可以考慮把秩看作深度 跑lca

#include using

namespace

std;

const

int maxn = 5e5 + 5

;int

n, m, cnt;

intfa[maxn];

intid[maxn];

intzhi[maxn];

int find(int

x) int ffind(int

x) void add(int x, int y, int

z)

else fa[fy] = fx, id[fy] =z;

if(zhi[fx] == zhi[fy]) zhi[fx]++;

}}int query(int x, int

y)

return

res;

}int

main()

return0;

}

view code

BZOJ4668 冷戰 並查集

time limit 10 sec memory limit 256 mb submit 538 solved 269 submit status discuss 1946 年 3 月 5 日,英國前首相溫斯頓 邱吉爾在美國富爾頓發表 鐵 幕演說 正式拉開了冷戰序幕。美國和蘇聯同為世界上的 超級大國...

BZOJ4668 冷戰 並查集

比較自然的思路是,由於需要記錄連通塊合併時的資訊,所以需要建出kruskal重構樹。不妨考慮在並查集合並的同時記錄資訊,pre x 表示x與它的父親相連的時刻。兩個點連通的時刻,等於兩個點之間路徑上時刻的最大值。注意到按秩合併但不路徑壓縮的並查集不改變樹的結構,且樹高為log,正好符合要求。o n ...

BZOJ4668 冷戰 並查集

time limit 10 sec memory limit 256 mb submit 538 solved 269 submit status discuss 1946 年 3 月 5 日,英國前首相溫斯頓 邱吉爾在美國富爾頓發表 鐵 幕演說 正式拉開了冷戰序幕。美國和蘇聯同為世界上的 超級大國...