BZOJ1969 [AHIO2005]航线规划

题目蓝链

Description

给定一个无向图,需要支持两个操作,断掉一条边或者询问两个点之间的路径上有多少个桥

Solution

考虑把操作离线,然后时光倒流一下。初始的时候所有的边权值都为\(1\),把加入边后形成的环直接置成\(0\)。询问就直接查询两点之间的链上权值和

Code

#include <bits/stdc++.h>

using namespace std;

#define fst first
#define snd second
#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

inline int read() {
    int sum = 0, fg = 1; char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
    for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
    return fg * sum;
}

const int maxn = 3e4 + 10;
const int maxm = 2e5 + 10;

int n, m, cnt;

vector<int> g[maxn];
multiset<pii> Set;

struct node {
    int x, y, ty;
}tt[maxm];

int Fa[maxn], S[maxn];
int find(int x) { return x == Fa[x] ? x : Fa[x] = find(Fa[x]); }

namespace ST {
#define ls (rt << 1)
#define rs (rt << 1 | 1)
    int A[maxn << 2], tag[maxn << 2];
    void push_up(int rt) { A[rt] = A[ls] + A[rs]; }
    void push_down(int rt, int l, int r) {
        if (~tag[rt]) {
            tag[ls] = tag[rs] = tag[rt];
            int mid = (l + r) >> 1;
            A[ls] = tag[rt] * (mid - l + 1);
            A[rs] = tag[rt] * (r - mid);
            tag[rt] = -1;
        }
    }
    void change(int rt, int l, int r, int L, int R, int v) {
        if (L <= l && r <= R) {
            A[rt] = v * (r - l + 1), tag[rt] = v;
            return;
        }
        push_down(rt, l, r);
        int mid = (l + r) >> 1;
        if (L <= mid) change(ls, l, mid, L, R, v);
        if (R > mid) change(rs, mid + 1, r, L, R, v);
        push_up(rt);
    }
    int query(int rt, int l, int r, int L, int R) {
        if (L <= l && r <= R) return A[rt];
        push_down(rt, l, r);
        int mid = (l + r) >> 1, res = 0;
        if (L <= mid) res += query(ls, l, mid, L, R);
        if (R > mid) res += query(rs, mid + 1, r, L, R);
        return res;
    }
    void init() {
        memset(tag, -1, sizeof tag);
        change(1, 1, n, 1, n, 1);
    }
}

int d[maxn], fa[maxn], sz[maxn], top[maxn], hs[maxn];
int Index, dfn[maxn];

void dfs1(int now, int f) {
    d[now] = d[f] + 1, fa[now] = f, sz[now] = 1;
    int ms = 0;
    for (int i = 0; i < g[now].size(); i++) {
        int son = g[now][i];
        if (son == f) continue;
        dfs1(son, now);
        sz[now] += sz[son];
        if (sz[son] > sz[ms]) ms = son;
    }
    hs[now] = ms;
}

void dfs2(int now, int topf) {
    dfn[now] = ++Index;
    top[now] = topf;
    if (!hs[now]) return;
    dfs2(hs[now], topf);
    for (int i = 0; i < g[now].size(); i++) {
        int son = g[now][i];
        if (son == fa[now] || son == hs[now]) continue;
        dfs2(son, son);
    }
}

void change(int x, int y, int v) {
    while (top[x] != top[y]) {
        if (d[top[x]] < d[top[y]]) swap(x, y);
        ST::change(1, 1, n, dfn[top[x]], dfn[x], v);
        x = fa[top[x]];
    }
    if (d[x] > d[y]) swap(x, y);
    if (dfn[x] < dfn[y]) ST::change(1, 1, n, dfn[x] + 1, dfn[y], v);
}

int query(int x, int y) {
    int res = 0;
    while (top[x] != top[y]) {
        if (d[top[x]] < d[top[y]]) swap(x, y);
        res += ST::query(1, 1, n, dfn[top[x]], dfn[x]);
        x = fa[top[x]];
    }
    if (d[x] > d[y]) swap(x, y);
    if (dfn[x] < dfn[y]) res += ST::query(1, 1, n, dfn[x] + 1, dfn[y]);
    return res;
}

int main() {
    freopen("line.in", "r", stdin);
    freopen("line.out", "w", stdout);

    n = read(), m = read();
    for (int i = 1; i <= m; i++) {
        int x = read(), y = read();
        if (x > y) swap(x, y);
        Set.insert((pii){x, y});
    }

    int op;
    while (~(op = read())) {
        int x = read(), y = read();
        if (x > y) swap(x, y);
        if (!op) Set.erase(Set.lower_bound((pii){x, y}));
        tt[++cnt] = (node){x, y, op};
    }

    for (int i = 1; i <= n; i++) Fa[i] = i;
    for (multiset<pii>::iterator it = Set.begin(); it != Set.end(); ++it) {
        int x = it->fst, y = it->snd;
        if (find(x) == find(y)) { tt[++cnt] = (node){x, y, 0}; continue; }
        Fa[find(x)] = find(y);
        g[x].push_back(y), g[y].push_back(x);
    }

    dfs1(1, 0);
    dfs2(1, 1);

    ST::init();
    for (int i = cnt; i >= 1; i--) {
        if (tt[i].ty) S[++S[0]] = query(tt[i].x, tt[i].y);
        else change(tt[i].x, tt[i].y, 0);
    }

    for (int i = S[0]; i >= 1; i--) printf("%d\n", S[i]);

    return 0;
}

转载于:https://www.cnblogs.com/xunzhen/p/9971783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值