bzoj2959 長跑 LCT 並查集

2022-05-08 17:39:08 字數 1289 閱讀 8538

動態樹link-cut tree(lct)總結

lct常數大得真實

沒有環,就是\(lct\)裸題吧

有環,我們就可以繞環轉一圈,縮點

怎麼搞?

當形成環時,把所有點的值全部加到乙個點上,用並查集維護加到哪個點上

判斷連通性再用乙個並查集

#include#define ll long long

#define rg register

using namespace std;

templateinline void read(t &x)

templateinline void write(t x)

if (x < 0) x = -x, putchar('-');

int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;

for (rg int i = len; i >= 0; i--) putchar(z[i]+48);return ;

}const int n = 150010;

struct node t[n];

int s[n], top;

#define lson (t[x].ch[0])

#define rson (t[x].ch[1])

void pushup(int x)

void putrev(int x)

void pushdown(int x)

}int fa[n], fa[n];

int find(int x)

int find(int x)

bool isroot(int x)

void rotate(int x)

void splay(int x)

pushup(x);

return ;

}void access(int x)

void makeroot(int x)

void link(int x, int y)

void split(int x, int y)

int a[n], n, m;

void dfs(int x, int y)

int main()

else

} else if (op == 2)

else

split(x, y);

printf("%d\n", t[y].s);

} }

return 0;

}

bzoj 2959 長跑(LCT 並查集)

time limit 10 sec memory limit 256 mb submit 315 solved 178 submit status discuss 某校開展了同學們喜聞樂見的陽光長跑活動。為了能 為祖國健康工作五十年 同學們紛紛離開寢室,離開教室,離開實驗室,到操場參加3000公尺長...

BZOJ2959 長跑(lct 並查集)

傳送門 用lct維護一顆動態樹。如果連了某一條邊形成了乙個環,證明一次長跑這個環上的所有的點都可以被統計,所以可以將這個環縮成乙個點。用ufs來實現。那麼一次長跑實際上就是在一條樹鏈上跑,只有乙個方向,在lct上維護乙個sum就可以了。時間複雜度是均攤的,因為每乙個點至多被縮點一次,所以o k ml...

bzoj2959 長跑 LCT 並查集

某校開展了同學們喜聞樂見的陽光長跑活動。為了能 為祖國健康工作五十年 同學們紛紛離開寢室,離開教室,離開實驗室,到操場參加3000公尺長跑運動。一時間操場上熙熙攘攘,摩肩接踵,盛況空前。為了讓同學們更好地監督自己,學校推行了刷卡機制。學校中有n個地點,用1到n的整數表示,每個地點設有若干個刷卡機。有...