[BZOJ3669][NOI2014]魔法森林(LCT)

来源:互联网 发布:浪漫主义文学 知乎 编辑:程序博客网 时间:2024/05/18 02:19

直接求解比较麻烦。考虑先按照Ai为关键字将每条边排序,然后按顺序动态加入每一条边。以下定一条路径的代价为一条路径经过的所有边的B的最大值。可以知道,加入第i条边之后的最小代价,就是Ai加上从1n的所有路径中,代价最小的路径的代价。
看到动态加边,可以想到使用LCT来完成。由于要维护边权,所以要把边在LCT中理解成点。
但是现在的一个问题是出现环时应该怎样处理。假设现在要连边(u,v),但uv之间已经存在一条路径。这样就必须在uv的路径中选一条边断开。由于操作后要求得最小代价,因此,应该在uv的路径中,选择一条边权最大的边断开后再连边(u,v)。当然,如果uv在连边之前不连通,就不必要做这个步骤了。
连边之后,即可询问1n的路径代价,加上Ai并更新答案。
代码:

#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;}
原创粉丝点击