TYVJ2019 Freda的传呼机

题面

为了随时与rainbow快速交流,Freda制造了两部传呼机。Freda和rainbow所在的地方有N座房屋、M 条双向光缆。

每条光缆连接两座房屋,传呼机发出的信号只能沿着光缆传递,并且传呼机的信号从光缆的其中一端传递到另一端需要花费t单位时间。

现在Freda要进行Q次试验,每次选取两座房屋,并想知道传呼机的信号在这两座房屋之间传递至少需要多长时间。

N座房屋通过光缆一定是连通的,并且这M条光缆有以下三类连接情况:

A:光缆不形成环,也就是光缆仅有N-1 条。
B:光缆只形成一个环,也就是光缆仅有N 条。
C:每条光缆仅在一个环中。

请你帮帮他们。

输入格式

第一行包含三个用空格隔开的整数,N、M 和Q。

接下来M行每行三个整数x、y、t,表示房屋x和y之间有一条传递时间为 t 的光缆。

最后Q行每行两个整数x、y,表示Freda想知道在x和y之间传呼最少需要多长时间。

输出格式

输出Q行,每行一个整数,表示Freda每次试验的结果。

数据范围

2≤N≤10000,
N−1≤M≤12000,
Q=10000,
1≤x,y≤N,
1≤t<32768

输入样例:

5 4 2
1 2 1
1 3 1
2 4 1
2 5 1
3 5
2 1

输出样例:

3
1

题解

我觉得蓝书 把这道题基环树这里有点不合适, 放到连通性tarjan那里是在合适不过

仙人掌图, 也就是特殊的无向图v-dcc 点双缩点罢了, 无非是一般点双缩点缩点内存在多个环, 内部不好处理

而仙人掌是缩点内只有一个环, 内部处理距离很好做

还有的区别是, 普通点双缩点,在缩点内且不为割点的点只是属于这个点双, 而不会去将 点双内部的点和缩点建边

剩下的无非是 树上LCA的板子

至于怎么处理环内距离, 你要是自己弄懂了tarjan, 看看代码也就懂了, 所以~

强烈建议先把无向图的连通性那一张学了

//每条边至多在1个环上
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;

const int N = 1e5 + 5, M = 1e5 + 5;

int n, m, _, k;
int h[N], to[M << 1], ne[M << 1], co[M << 1], bl[M], tot;
int hc[N << 1], tc[M << 1], nc[M << 1], cc[M << 1], totc;
int dfn[N], low[N], df, st[N], top, root, fa[N << 1];
int vcnt, sum[N << 1];
int f[N << 1][21], dep[N << 1], dist[N << 1], t;

void add(int u, int v, int c) {
    ne[++tot] = h[u]; to[h[u] = tot] = v; co[tot] = c;
}

void add_c(int u, int v, int c) {
    nc[++totc] = hc[u]; tc[hc[u] = totc] = v; cc[totc] = c;
}

void solve(int x, int y, int c) {
    sum[++vcnt + n] = sum[y] = c;
    for (int i = y; i != x; i = fa[i]) sum[fa[i]] = sum[vcnt + n] += dist[i];
    sum[x] = 0;
    for (int i = y; i != fa[x]; i = fa[i]) {
        int c = min(sum[i], sum[vcnt + n] - sum[i]);
        add_c(i, vcnt + n, c); add_c(vcnt + n, i, c);
    }
}

void tarjan(int x) {
    dfn[x] = low[x] = ++df;
    if (!h[x]) return;
    st[++top] = x;
    for (int i = h[x]; i; i = ne[i]) {
        int y = to[i];
        if (!dfn[y]) {
            dist[y] = co[i]; fa[y] = x;
            tarjan(y);
            low[x] = min(low[x], low[y]);

            if (dfn[x] <= low[y]) {
                int z = st[top], c;
                while (y != st[top--]);
                for (int j = h[z]; j; j = ne[j])
                    if (to[j] == x) { c = co[j]; break; }
                solve(x, z, c);
            }
        }
        else low[x] = min(low[x], dfn[y]);
    }
}

void bfs(int s) {
    queue<int> q;
    q.push(s); dep[s] = 1; dist[s] = 0;
    rep(i, 0, t) f[s][i] = 0;

    while (!q.empty()) {
        int x = q.front(); q.pop();
        for (int i = hc[x]; i; i = nc[i]) {
            int y = tc[i];
            if (dep[y]) continue;
            dep[y] = dep[x] + 1;
            dist[y] = dist[x] + cc[i];
            f[y][0] = x;

            for (int j = 1; j <= t; ++j)
                f[y][j] = f[f[y][j - 1]][j - 1];
            q.push(y);
        }
    }
}

int lca(int x, int y) {
    if (dep[x] > dep[y]) swap(x, y);
    per(i, t, 0)
        if (dep[f[y][i]] >= dep[x]) y = f[y][i];
    if (x == y) return x;
    per(i, t, 0)
        if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];

    return f[x][0];
}

int main() {
    IOS;
    while (cin >> n >> m >> k) {
        tot = 1; totc = df;
        rep(i, 1, n) {
            h[i] = dfn[i] = dep[i] = 0;
            hc[i] = hc[i + n] = 0;
            dist[i] = dist[i + n] = 0;
        }
        rep(i, 1, m) {
            int u, v, c; cin >> u >> v >> c;
            add(u, v, c); add(v, u, c);
        }

        rep (i, 1, n) if (!dfn[i]) top = 0, root = i, tarjan(i);
        t = log2(vcnt + n - 1) + 1;
        rep(i, 1, n) if (!dep[i]) bfs(i);

        //cout << vcnt << '
';

        rep(i, 1, k) {
            int x, y; cin >> x >> y;
            int lc = lca(x, y);
            if (lc <= n) cout << dist[x] + dist[y] - (dist[lc] << 1) << '
';
            else {
                int a = x, b = y;
                per(j, t, 0) if (dep[f[x][j]] >= dep[lc] + 1) x = f[x][j];
                per(j, t, 0) if (dep[f[y][j]] >= dep[lc] + 1) y = f[y][j];
                int mi = abs(sum[x] - sum[y]); mi = min(mi, sum[lc] - mi);
                cout << dist[a] - dist[x] + dist[b] - dist[y] + mi << '
';
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/2aptx4869/p/13712282.html