鏈接:https://www.lydsy.com/JudgeOnline/problem.php?id=4736
題面:
寒冬又一次肆虐了北國大地
無情的北風穿透了人們御寒的衣物
可憐蟲們在冬夜中發出無助的哀嚎
“凍死寶寶了!”
這時
遠處的天邊出現了一位火焰之神
“我將賜予你們溫暖和希望!”
只見他的身體中噴射出火焰之力
通過堅固的鋼鐵,傳遍了千家萬戶
這時,只聽見人們歡呼
“暖氣來啦!”
任務描述
雖然小R住的宿舍樓早已來了暖氣,但是由于某些原因,宿舍樓中的某些窗戶仍然開著(例如廁所的窗戶),這就使得宿舍樓中有一些路上的溫度還是很低。
小R的宿舍樓中有nn個地點和一些路,一條路連接了兩個地點,小R可以通過這條路從其中任意一個地點到達另外一個地點。但在剛開始,小R還不熟悉宿舍樓中的任何一條路,所以他會慢慢地發現這些路,他在發現一條路時還會知道這條路的溫度和長度。每條路的溫度都是互不相同的。
小R需要在宿舍樓中活動,每次他都需要從一個地點到達另一個地點。小R希望每次活動時經過一條最溫暖的路徑,最溫暖的路徑的定義為,將路徑上各條路的溫度從小到大排序后字典序最大。即溫度最低的路溫度盡量高,在滿足該條件的情況下,溫度第二低的路溫度盡量高,以此類推。小R不會經過重復的路。由于每條路的溫度互不相同,因此只存在一條最溫暖的路徑。
對于小R的每次活動,你需要求出小R需要走過的路徑總長度。如果小R通過當前發現的路不能完成這次活動,則輸出??1?1。
注意本題中的字典序與傳統意義上的字典序定義有所不同,對于兩個序列a,b(a≠b)a,b(a≠b),若aa是bb的前綴則aa的字典序較大,同時可以推出空串的字典序最大。
輸入格式
第一行兩個正整數?n,mn,m。表示小R的宿舍樓中有?nn?個地點,共發生了?mm?個事件。
接下來?mm?行,每行描述一個事件,事件分為三類。
-
find?id?u?v?t?lfind id u v t l?表示小R發現了一條連接uu和vv之間的路,編號為idid。相同idid的邊只會出現一次。
-
move?u?vmove u v?表示小R要從uu到達vv,你需要計算出最溫暖的路徑的長度 ,若不能從uu到達vv,則輸出?1?1。
-
change?id?lchange id l?表示從uu到vv這條邊的長度變為了ll(保證在當前時間點這條邊存在)。
輸出格式
對于每個詢問,輸出一行整數,表示最溫暖的路徑長度。
樣例一
input
8 19 find 0 0 2 7 2 find 1 2 4 4 4 find 2 4 6 10 1 find 3 6 7 8 6 move 2 7 move 1 6 find 4 2 5 3 4 move 0 5 change 0 12 find 5 4 5 5 10 find 6 2 3 6 9 move 3 5 find 7 0 1 12 1 move 1 6 find 8 1 7 11 100 move 1 6 move 3 7 move 5 6 move 2 2
output
11 -1 6 23 18 106 122 11 0
樣例二
input
15 45 find 0 1 0 8 5987 find 1 2 0 14 5455 find 2 3 0 27 8830 find 3 4 3 42 7688 find 4 5 0 25 1756 find 5 6 5 35 1550 find 6 7 4 43 9440 move 3 9 change 2 9113 move 10 13 move 3 3 move 11 10 find 7 8 7 6 7347 find 8 9 8 26 8935 move 8 4 change 3 4466 find 9 10 9 28 8560 move 6 5 find 10 11 10 31 6205 change 9 9228 find 11 12 10 23 948 find 12 13 12 45 5945 move 0 9 move 2 5 change 2 6118 find 13 14 13 12 6906 move 4 1 change 2 504 find 14 4 2 22 9796 move 10 7 move 1 14 move 13 3 find 15 12 9 39 8985 find 16 9 8 17 3710 change 1 5370 find 17 1 0 36 4669 find 18 7 6 37 8087 move 9 0 find 19 14 9 33 8234 find 20 0 4 24 5209 change 1 4883 find 21 6 3 9 2461 find 22 5 2 19 4291 change 1 7219 change 6 4846
output
-1 -1 0 -1 16787 1550 39301 7211 16571 25510 59706 46309 30692
?思路:
題面說了一大推,其實就是求最大生成樹,用LCT維護下就好了
?
實現代碼:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set> #include<map> #include<vector> #include<queue> using namespace std; #define ls c[x][0] #define rs c[x][1] const int M = 4e5+10; const int inf = 1e9; int top; int sum[M],c[M][2],val[M],fa[M],rev[M],mn[M],S[M],tmp[M]; inline void up(int x){sum[x] = sum[ls] + sum[rs] + val[x];mn[x] = x;if(ls&&tmp[mn[ls]] < tmp[mn[x]]) mn[x] = mn[ls];if(rs&&tmp[mn[rs]] < tmp[mn[x]]) mn[x] = mn[rs]; }inline bool isroot(int x){return c[fa[x]][0]!=x&&c[fa[x]][1]!=x; }inline void rotate(int x){int y = fa[x],z = fa[y];int k = c[y][1] == x;if(!isroot(y)) c[z][c[z][1]==y]=x;fa[x] = z;c[y][k] = c[x][k^1]; fa[c[x][k^1]] = y;c[x][k^1] = y; fa[y] = x;up(y); up(x); }inline void pushdown(int x){if(!rev[x]) return ;rev[ls]^=1; rev[rs]^=1;rev[x]^=1;swap(ls,rs); }inline void splay(int x){S[top=1]=x;for(int i = x;!isroot(i);i=fa[i]) S[++top] = fa[i];while(top) pushdown(S[top--]);while(!isroot(x)){int y = fa[x],z = fa[y];if(!isroot(y))(c[y][1]==x)^(c[z][1]==y)?rotate(x):rotate(y);rotate(x);} }inline void access(int x){for(int y = 0;x;y = x,x = fa[x])splay(x),c[x][1] = y,up(x); }inline void makeroot(int x){access(x); splay(x); rev[x] ^= 1; }inline void split(int x,int y){makeroot(x); access(y); splay(y); }inline void link(int x,int y){makeroot(x);fa[x] = y; }inline void cut(int x,int y){split(x,y); fa[x] = c[y][0] = 0; up(y); }inline int findroot(int x){access(x); splay(x);while(ls) x = ls;return x; } int main() {int n,m;char op[15];scanf("%d%d",&n,&m);for(int i = 0;i <= n;i ++) tmp[i] = inf;while(m--){scanf("%s",op);if(op[0] == 'f') {int id,u,v,t,len;scanf("%d%d%d%d%d",&id,&u,&v,&t,&len);id++; u++; v++;tmp[id+n] = t; val[id+n] = len;if(findroot(u) != findroot(v)) link(u,id+n),link(v,id+n);else{split(u,v);// cout<<"tmp :"<<tmp[mn[v]]<<" "<<t<<" "<<val[id+n]<<endl;if(tmp[mn[v]] < t){int kk = mn[v];cut(u,kk); cut(v,kk);link(u,id+n); link(v,id+n);}}}else if(op[0] == 'm'){int u,v;scanf("%d%d",&u,&v);u++; v++;if(findroot(u) != findroot(v)) printf("-1\n");else split(u,v),printf("%d\n",sum[v]);}else {int id,len;scanf("%d%d",&id,&len); id++;makeroot(id+n); val[id+n] = len; up(id+n);}}return 0; }
?