日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

loj2245 [NOI2014]魔法森林 LCT

發布時間:2023/11/30 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 loj2245 [NOI2014]魔法森林 LCT 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[NOI2014]魔法森林

鏈接

loj

思路

a排序,b做動態最小生成樹。
把邊拆成點就可以了。
uoj98.也許lct復雜度寫假了、、越卡常,越慢

代碼

#include <bits/stdc++.h> #define ls c[x][0] #define rs c[x][1] using namespace std; const int N = 2e5 + 7; int read() {int x = 0, f = 1; char s = getchar();for (;s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;for (;s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';return x * f; } struct edge {int x, y, a, b;bool operator < (const edge &zz) const {return (a^zz.a) ? a < zz.a : b < zz.b;} } G[N]; int f[N], c[N][2], w[N][2], ma[N][2], stak[N], lazy[N], id[N]; bool isroot(int x) {return c[f[x]][0] == x || c[f[x]][1] == x;} void pushup(int x) {ma[x][0] = max(max(ma[ls][0], ma[rs][0]), w[x][0]);ma[x][1] = max(max(ma[ls][1], ma[rs][1]), w[x][1]);id[x] = (ma[x][1] == w[x][1]) ? x : (ma[ls][1] > ma[rs][1]) ? id[ls] : id[rs]; } void tag(int x){swap(ls,rs), lazy[x] ^= 1;} void pushdown(int x) {if (lazy[x]) {if (ls) tag(ls);if (rs) tag(rs);lazy[x] ^= 1;} } void rotate(int x) {int y = f[x], z = f[y], k = c[y][1] == x, w = c[x][!k];if (isroot(y)) c[z][c[z][1] == y] = x;c[x][!k] = y, c[y][k] = w;if (w) f[w] = y;f[x] = z, f[y] = x;pushup(y); } void splay(int x) {int y = x, z = 0;stak[++z] = y;while (isroot(y)) stak[++z] = y = f[y];while (z) pushdown(stak[z--]);while (isroot(x)) {y = f[x], z = f[y];if (isroot(y)) rotate((c[y][0] == x)^(c[z][0] == y) ? x : y);rotate(x);}pushup(x); } void access(int x) {for (int y = 0; x;x = f[y = x])splay(x), rs = y, pushup(x); } void makeroot(int x) {access(x), splay(x);tag(x); } int findroot(int x) {access(x), splay(x);while(ls) pushdown(x), x = ls;return x; } void split(int x, int y) {makeroot(x), access(y), splay(y); } void link(int x, int y) {makeroot(x);if (findroot(y) != x) f[x] = y; } void cut(int x, int y) {makeroot(x);if (findroot(y) == x && f[x] == y && !rs) {f[x] = c[y][0] = 0;pushup(y);} } int main() {int n = read(), m = read(), ans = 0x3f3f3f3f;for (int i = 1; i <= m; ++i)G[i].x = read(), G[i].y = read(), G[i].a = read(), G[i].b = read();sort(G + 1, G + 1 + m);for (int i = 1; i <= m; ++i) {if (G[i].x == G[i].y) continue;int x = G[i].x, y = G[i].y;if (findroot(x) == findroot(y)) {split(x, y);if (ma[y][1] > G[i].b) {int tmp = id[y];cut(G[tmp - n].x, tmp), cut(G[tmp - n].y, tmp);w[n + i][0] = G[i].a, w[n + i][1] = G[i].b;link(x, n + i), link(n + i, y);}} else {w[n + i][0] = G[i].a, w[n + i][1] = G[i].b;link(x, n + i), link(n + i, y);}if (findroot(1) == findroot(n)) {split(1, n);ans = min(ans, ma[n][0] + ma[n][1]);}}printf("%d\n", ans == 0x3f3f3f3f ? -1 : ans);return 0; }

轉載于:https://www.cnblogs.com/dsrdsr/p/10959020.html

總結

以上是生活随笔為你收集整理的loj2245 [NOI2014]魔法森林 LCT的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。