网络

AcWing-364-网络open in new window

分析

见《进阶指南》第407页。

实现

#include <iostream>
#include <cstring>
#define size SiZe
using namespace std;

const int N = 100010, M = 200010;
struct edge {
    int to, next;
};
edge e[2 * M];
int idx, head[N];
struct node {
    int u, v;
};
int n, m, q;
node rec[M];
int dep[N], fa[N], size[N];
int heavy_son[N], top[N];
int cnt, dfn[N], low[N];
bool bridge[2 * M];
int tot, dcc[N];
int f[N];

void add_edge (int u, int v) {
    e[idx].to = v;
    e[idx].next = head[u];
    head[u] = idx ++;
}
void dfs1 (int cur, int father) {
    size[cur] = 1;
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == father) continue;
        dep[to] = dep[cur] + 1;
        fa[to] = cur;
        dfs1(to, cur);
        size[cur] += size[to];
        if (size[to] > size[heavy_son[cur]])
            heavy_son[cur] = to;
    }
}
void dfs2 (int cur, int top_node) {
    top[cur] = top_node;
    if (heavy_son[cur]) dfs2(heavy_son[cur], top_node);
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == fa[cur] || to == heavy_son[cur]) continue;
        dfs2(to, to);
    }
}
int lca (int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    return x;
}
void initialize () {
    idx = 0;
    memset(head, -1, sizeof(head));
    cnt = 0;
    memset(dfn, 0, sizeof(dfn));
    memset(bridge, 0, sizeof(bridge));
    tot = 0;
    memset(dcc, 0, sizeof(dcc));
    memset(dep, 0, sizeof(dep));
    memset(fa, 0, sizeof(fa));
    for (int i = 1; i <= n; ++ i) f[i] = i;
}
void tarjan (int cur, int eid) {
    low[cur] = dfn[cur] = ++ cnt;
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (dfn[to] == 0) {
            tarjan(to, i);
            low[cur] = min(low[cur], low[to]);

            if (low[to] > dfn[cur])
                bridge[i] = bridge[i ^ 1] = true;
        } else if (i != (eid ^ 1))
            low[cur] = min(low[cur], dfn[to]);
    }
}
void dfs3 (int cur) {
    dcc[cur] = tot;
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (bridge[i] == true) continue;
        if (dcc[to] == 0) dfs3(to);
    }
}
int find (int x) {
    if (f[x] == x) return x;
    return f[x] = find(f[x]);
}

int main () {
    int T = 0;
    while (scanf("%d%d", &n, &m), n || m) {
        initialize();
        for (int i = 1, u, v; i <= m; ++ i) {
            scanf("%d%d", &u, &v);
            add_edge(u, v);
            add_edge(v, u);
            rec[i] = { u, v };
        }
        scanf("%d", &q);
        printf("Case %d:\n", ++ T);

        tarjan(1, -1);
        for (int i = 1; i <= n; ++ i) {
            if (dcc[i] == 0) {
                ++ tot;
                dfs3(i);
            }
        }
        idx = 0;
        memset(head, -1, sizeof(head));
        for (int i = 1; i <= m; ++ i) {
            int u = rec[i].u, v = rec[i].v;
            if (dcc[u] == dcc[v]) continue;
            add_edge(dcc[u], dcc[v]);
            add_edge(dcc[v], dcc[u]);
        }
        dfs1(1, -1);
        dfs2(1, 1);
        int res = tot - 1;
        while (q --) {
            int u, v, a;
            scanf("%d%d", &u, &v);
            u = dcc[u], v = dcc[v], a = lca(u, v);

            u = find(u);
            while (dep[u] > dep[a]) {
                -- res;
                f[u] = fa[u];
                u = find(u);
            }

            v = find(v);
            while (dep[v] > dep[a]) {
                -- res;
                f[v] = fa[v];
                v = find(v);
            }
            printf("%d\n", res);
        }
        printf("\n");
    }
    return 0;
}
最后修改于: