1 条题解
-
1
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; inline int read() { int res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-'); if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + (c - 48); return bo ? ~res + 1 : res; } const int N = 2e5 + 5; int n, m, fa[N], lc[N], rc[N], rev[N], que[N], len, val[N], sm[N], f[N]; struct cyx {int a, b, x, y;} ask[N]; int cx(int x) { if (f[x] != x) f[x] = cx(f[x]); return f[x]; } void zm(int x, int y) { int ix = cx(x), iy = cx(y); if (ix != iy) f[iy] = ix; } bool comp(cyx a, cyx b) {return a.y < b.y;} int which(int x) {return rc[fa[x]] == x;} bool is_root(int x) { return !fa[x] || (lc[fa[x]] != x && rc[fa[x]] != x); } void upt(int x) { sm[x] = x; if (lc[x] && val[sm[lc[x]]] > val[sm[x]]) sm[x] = sm[lc[x]]; if (rc[x] && val[sm[rc[x]]] > val[sm[x]]) sm[x] = sm[rc[x]]; } void down(int x) { if (rev[x]) { swap(lc[x], rc[x]); if (lc[x]) rev[lc[x]] ^= 1; if (rc[x]) rev[rc[x]] ^= 1; rev[x] = 0; } } void rotate(int x) { int y = fa[x], z = fa[y], b = lc[y] == x ? rc[x] : lc[x]; if (z && !is_root(y)) (lc[z] == y ? lc[z] : rc[z]) = x; fa[x] = z; fa[y] = x; b ? fa[b] = y : 0; if (lc[y] == x) rc[x] = y, lc[y] = b; else lc[x] = y, rc[y] = b; upt(y); upt(x); } void splay(int x) { int i, y; que[len = 1] = x; for (y = x; !is_root(y); y = fa[y]) que[++len] = fa[y]; for (i = len; i >= 1; i--) down(que[i]); while (!is_root(x)) { if (!is_root(fa[x])) { if (which(x) == which(fa[x])) rotate(fa[x]); else rotate(x); } rotate(x); } upt(x); } void Access(int x) { int y; for (y = 0; x; y = x, x = fa[x]) { splay(x); rc[x] = y; if (y) fa[y] = x; upt(x); } } int Find_Root(int x) { Access(x); splay(x); while (down(x), lc[x]) x = lc[x]; splay(x); return x; } void Make_Root(int x) { Access(x); splay(x); rev[x] ^= 1; } void Link(int x, int y) { Make_Root(x); fa[x] = y; } void Cut(int x, int y) { Make_Root(x); Access(y); splay(y); lc[y] = 0; fa[x] = 0; upt(y); } int Select(int x, int y) { Make_Root(x); Access(y); splay(y); return sm[y]; } int main() { int i, ans = 2e9; n = read(); m = read(); for (i = 1; i <= m; i++) ask[i].a = read(), ask[i].b = read(), ask[i].x = read(), ask[i].y = read(); sort(ask + 1, ask + m + 1, comp); for (i = 1; i <= n + m; i++) f[i] = sm[i] = i; for (i = n + 1; i <= n + m; i++) val[i] = ask[i - n].x; for (i = 1; i <= m; i++) { int u = ask[i].a, v = ask[i].b; bool flag = 1; if (cx(u) == cx(v)) { int w = Select(u, v); if (val[w] > ask[i].x) Cut(ask[w - n].a, w), Cut(w, ask[w - n].b); else flag = 0; } else zm(u, v); if (flag) Link(u, i + n), Link(i + n, v); if (cx(1) == cx(n)) ans = min(ans, ask[i].y + val[Select(1, n)]); } if (ans < 2e9) printf("%d\n", ans); else printf("-1\n"); return 0; }
- 1
信息
- ID
- 1335
- 时间
- 3000ms
- 内存
- 125MiB
- 难度
- 6
- 标签
- 递交数
- 6
- 已通过
- 2
- 上传者