【LG3250】[HNOI2016]网络

【LG3250】[HNOI2016]网络

题面

洛谷

题解

\(30pts\)

对于\(m\leq 2000\),直接判断一下这个个点是否断掉一个交互,没断掉的里面取\(max\)即可,复杂度\(O(m^2\log n)\)。

另\(20pts\)

对于无删除操作的,用线段树维护,
我们将一条路径的补集全部打上那条路径重要度的标记,这样我们断一条边时直接单点查询即可。

据说这样子再改改可以变成一种树剖加堆的做法,但是好像现在在bzoj被卡了

\(100pts\)

想到二分答案,若所有权值\(\geq mid\)的路径都过\(x\),那么\(ans<mid\),反之若有不过\(x\)的,则说明\(ans\geq mid\)。

这个可以用树套树维护,因为树状数组套线段树会炸空间,所以就线段树套平衡树,复杂度\(O(m\log ^3n)\)。

考虑整体二分,那么我们按照套路按时间加入,将一条路径树上差分,
碰到一次查询直接看经过它的路径条数即可,复杂度\(O(m\log^2 n)\)。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> 
#include <cmath> 
#include <algorithm>
using namespace std; 
inline int gi() {
    register int data = 0, w = 1;
    register char ch = 0;
    while (!isdigit(ch) && ch != '-') ch = getchar(); 
    if (ch == '-') w = -1, ch = getchar(); 
    while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); 
    return w * data; 
} 
const int MAX_N = 1e5 + 5, MAX_M = 2e5 + 5; 
struct Graph { int to, next; } e[MAX_N << 1]; int fir[MAX_N], e_cnt; 
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; } 
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}; fir[u] = e_cnt++; } 
int dep[MAX_N], fa[MAX_N], size[MAX_N], top[MAX_N], son[MAX_N];
int L[MAX_N], R[MAX_N], tim; 
void dfs1(int x) { 
    dep[x] = dep[fa[x]] + 1, size[x] = 1; 
    for (int i = fir[x]; ~i; i = e[i].next) { 
        int v = e[i].to; if (v == fa[x]) continue; 
        fa[v] = x, dfs1(v), size[x] += size[v];
        if (size[son[x]] < size[v]) son[x] = v; 
    } 
} 
void dfs2(int x, int tp) { 
    top[x] = tp, L[x] = ++tim; 
    if (son[x]) dfs2(son[x], tp); 
    for (int i = fir[x]; ~i; i = e[i].next) { 
        int v = e[i].to; if (v == son[x] || v == fa[x]) continue; 
        dfs2(v, v); 
    }
    R[x] = tim; 
}
int LCA(int u, int v) { 
    while (top[u] != top[v]) { 
        if (dep[top[u]] < dep[top[v]]) swap(u, v); 
        u = fa[top[u]]; 
    } 
    return dep[u] < dep[v] ? u : v; 
} 
int N, M; 
struct Option { int op, a, b, lca, v, id; } q[MAX_M], lq[MAX_M], rq[MAX_M]; 
int ans[MAX_M];
int c[MAX_N]; 
inline int lb(int x) { return x & -x; } 
int sum(int x) { int res = 0; while (x) res += c[x], x -= lb(x); return res; } 
void add(int x, int v) { while (x <= N) c[x] += v, x += lb(x); } 
void Div(int lval, int rval, int st, int ed) { 
    if (st > ed) return ; 
    if (lval == rval) { 
        for (int i = st; i <= ed; i++) 
            if (q[i].op == 2) ans[q[i].id] = lval; 
        return ; 
    } 
    int mid = (lval + rval) >> 1, lt = 0, rt = 0, cnt = 0; 
    for (int i = st; i <= ed; i++) { 
        if (q[i].op != 2) { 
            if (q[i].v > mid) { 
                int v = q[i].op ? -1 : 1; 
                cnt += v; 
                add(L[q[i].a], v), add(L[q[i].b], v), add(L[q[i].lca], -v);
                if (fa[q[i].lca]) add(L[fa[q[i].lca]], -v); 
                rq[++rt] = q[i]; 
            } else lq[++lt] = q[i]; 
        } else { 
            int res = sum(R[q[i].a]) - sum(L[q[i].a] - 1); 
            if (cnt <= res) lq[++lt] = q[i];
            else rq[++rt] = q[i]; 
        } 
    } 
    for (int i = 1; i <= rt; i++) {
        if (rq[i].op != 2) { 
            if (rq[i].v > mid) {
                int v = rq[i].op ? 1 : -1; 
                add(L[rq[i].a], v), add(L[rq[i].b], v), add(L[rq[i].lca], -v);
                if (fa[rq[i].lca]) add(L[fa[rq[i].lca]], -v); 
            } 
        } 
    } 
    for (int i = 1; i <= lt; i++) q[st + i - 1] = lq[i]; 
    for (int i = 1; i <= rt; i++) q[st + lt - 1 + i] = rq[i]; 
    Div(lval, mid, st, st + lt - 1);
    Div(mid + 1, rval, st + lt, ed); 
}
int h[MAX_N], cnt; 
int main () {
#ifndef ONLINE_JUDGE 
    freopen("cpp.in", "r", stdin);
#endif
    clearGraph(); 
    N = gi(), M = gi(); 
    for (int i = 1; i < N; i++) { 
        int u = gi(), v = gi(); 
        Add_Edge(u, v), Add_Edge(v, u); 
    }
    dfs1(1), dfs2(1, 1); 
    int q_cnt = 0; 
    for (int i = 1; i <= M; i++) { 
        int op = gi(); 
        if (op == 0) {
            int a = gi(), b = gi(); 
            q[i] = (Option){op, a, b, LCA(a, b), gi(), 0}; 
            h[++cnt] = q[i].v; 
        } 
        if (op == 1) q[i] = q[gi()], q[i].op = 1; 
        if (op == 2) q[i] = (Option){op, gi(), 0, 0, 0, ++q_cnt}; 
    }
    h[++cnt] = -1; 
    sort(&h[1], &h[cnt + 1]); cnt = unique(&h[1], &h[cnt + 1]) - h - 1; 
    for (int i = 1; i <= M; i++) if (q[i].op != 2) q[i].v = lower_bound(&h[1], &h[cnt + 1], q[i].v) - h; 
    Div(1, cnt, 1, M); 
    for (int i = 1; i <= q_cnt; i++) printf("%d\n", h[ans[i]]); 
    return 0; 
} 
上一篇:Python的shutil模块


下一篇:ActiveMQ入门代码