题面
解法
挺数据结构的一道题……
- 显然,对于删边我们并不是那么容易处理,那么我们就不妨把操作离线下来,倒着进行加边的操作
- 因为最后全部操作完之后保证图连通,那么我们可以对整张图按照边双缩点,然后整张图就显然地变成了一棵树
- 询问有多少条边删去后能使 x,y x , y 不连通,显然就是这棵树上 x,y x , y 的路径长
- 连接 x,y x , y 这条边,可以发现就是把 x,y x , y 这条路径上所有边的长度变成0
- 树剖+线段树实现即可
(尽管可以LCT) - 时间复杂度: O(mlog2n) O ( m log 2 n ) 或 O(mlogn) O ( m log n ) (LCT)
代码
#include <bits/stdc++.h>
#define mp make_pair
#define N 100010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
x = 0; int f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
int n, m, len, cnt, tot, Time;
int d[N], son[N], top[N], dfn[N], low[N], siz[N], col[N], f[N], st[N], in[N], ans[N], val[N], num[N], xx[N], yy[N];
vector <int> E[N];
struct Que {
int k, x, y, id;
} que[N];
struct Edge {
int next, num, v;
} e[N * 3];
struct SegmentTree {
struct Node {
int l, r, fl, sum;
} t[N * 4];
void update(int k) {t[k].sum = t[k << 1].sum + t[k << 1 | 1].sum;}
void build(int k, int l, int r) {
t[k] = (Node) {l, r, -1, 0};
if (l == r) {t[k].sum = val[num[l]]; return;}
int mid = (l + r) >> 1;
build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
update(k);
}
void pushdown(int k, int l, int r) {
int x = t[k].fl, mid = (l + r) >> 1; t[k].fl = -1;
int lc = k << 1, rc = k << 1 | 1;
t[lc].fl = t[rc].fl = x;
t[lc].sum = (mid - l + 1) * x, t[rc].sum = (r - mid) * x;
}
void modify(int k, int L, int R, int v) {
int l = t[k].l, r = t[k].r;
if (L <= l && r <= R) {
t[k].fl = v, t[k].sum = (r - l + 1) * v;
return;
}
if (t[k].fl != -1) pushdown(k, l, r);
int mid = (l + r) >> 1;
if (L <= mid && mid < R) modify(k << 1, L, mid, v), modify(k << 1 | 1, mid + 1, R, v);
if (R <= mid) modify(k << 1, L, R, v);
if (L > mid) modify(k << 1 | 1, L, R, v);
update(k);
}
int query(int k, int L, int R) {
if (L > R) return 0;
int l = t[k].l, r = t[k].r;
if (L <= l && r <= R) return t[k].sum;
if (t[k].fl != -1) pushdown(k, l, r);
int mid = (l + r) >> 1;
if (R <= mid) return query(k << 1, L, R);
if (L > mid) return query(k << 1 | 1, L, R);
return query(k << 1, L, mid) + query(k << 1 | 1, mid + 1, R);
}
} T;
void add(int x, int y, int v) {
e[++cnt] = (Edge) {e[x].next, y, v};
e[x].next = cnt;
}
void tarjan(int x, int las) {
dfn[x] = low[x] = ++Time;
st[++len] = x, in[x] = 1;
for (int i = 0; i < E[x].size(); i++) {
int k = E[x][i];
if (k == las) continue;
if (!dfn[k]) tarjan(k, x), chkmin(low[x], low[k]);
else if (in[k]) chkmin(low[x], dfn[k]);
}
if (low[x] == dfn[x]) {
col[x] = ++tot;
while (st[len] != x) col[st[len]] = tot, in[st[len--]] = 0;
in[st[len--]] = 0;
}
}
void dfs1(int x, int fa) {
siz[x] = 1, d[x] = d[fa] + 1, f[x] = fa;
for (int p = e[x].next; p; p = e[p].next) {
int k = e[p].num, v = e[p].v;
if (k == fa) continue; val[k] = v;
dfs1(k, x); siz[x] += siz[k];
if (siz[son[x]] < siz[k]) son[x] = k;
}
}
void dfs2(int x, int tp) {
top[x] = tp, dfn[x] = ++Time, num[Time] = x;
if (!son[x]) return; dfs2(son[x], tp);
for (int p = e[x].next; p; p = e[p].next) {
int k = e[p].num;
if (k == f[x] || k == son[x]) continue;
dfs2(k, k);
}
}
int lca(int x, int y) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (d[fx] < d[fy]) swap(fx, fy), swap(x, y);
x = f[fx], fx = top[x];
}
if (d[x] > d[y]) swap(x, y); return x;
}
void Modify(int x, int y, int v) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (d[fx] < d[fy]) swap(fx, fy), swap(x, y);
T.modify(1, dfn[fx], dfn[x], v);
x = f[fx], fx = top[x];
}
if (d[x] != d[y]) T.modify(1, dfn[y] + 1, dfn[x], v);
}
int Query(int x, int y) {
int fx = top[x], fy = top[y], ret = 0;
while (fx != fy) {
if (d[fx] < d[fy]) swap(x, y), swap(fx, fy);
ret += T.query(1, dfn[fx], dfn[x]);
x = f[fx], fx = top[x];
}
if (d[x] != d[y]) ret += T.query(1, dfn[y] + 1, dfn[x]);
return ret;
}
int main() {
read(n), read(m);
for (int i = 1; i <= m; i++)
read(xx[i]), read(yy[i]);
int k, q = 0, sum = 0; read(k);
map <pair <int, int>, int> h;
while (k != -1) {
int x, y; read(x), read(y);
que[++q] = (Que) {k, x, y, 0};
if (k == 0) h[make_pair(x, y)] = h[make_pair(y, x)] = 1;
else que[q].id = ++sum;
read(k);
}
for (int i = 1; i <= m; i++)
if (!h.count(make_pair(xx[i], yy[i]))) E[xx[i]].push_back(yy[i]), E[yy[i]].push_back(xx[i]);
tarjan(1, 0); cnt = n, Time = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j < E[i].size(); j++) {
int k = E[i][j];
if (col[i] == col[k]) continue;
add(col[i], col[k], 1);
}
dfs1(1, 0), dfs2(1, 0);
T.build(1, 1, tot);
for (int i = q; i; i--) {
int k = que[i].k, x = que[i].x, y = que[i].y;
if (k == 0) {
int z = lca(col[x], col[y]);
Modify(col[x], z, 0), Modify(col[y], z, 0);
} else {
int z = lca(col[x], col[y]);
ans[que[i].id] = Query(col[x], z) + Query(col[y], z);
}
}
for (int i = 1; i <= sum; i++) cout << ans[i] << "\n";
return 0;
}