题意
给你一个无向图,要求资瓷两个操作:删除一条边,或(x,y)表示询问有多少条边满足删掉后x和y不连通。
保证任意时刻该图连通。
n<=30000,m<=100000,q<=40000
分析
看到该图始终连通后,很容易想到先离线搞一棵生成树出来,那么答案的割边一定在这棵生成树上。
我们把询问离线,把删边变为加边,每加一条边就相当于把这条边两个端点路径上的边染成黑色,询问就相当于问两点路径上白色边的数量。用树剖和线段树维护即可。
树链剖分+线段树是真的长
代码
#include <bits/stdc++.h>
#define mp std::make_pair
const int N = 30005;
const int M = 200005;
int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int min(int x,int y)
{
return x < y ? x : y;
}
int max(int x,int y)
{
return x > y ? x : y;
}
struct Edge
{
int to,next;
int use,del;
}e[M];
int next[N];
int cnt;
void add(int x,int y)
{
e[++cnt].to = y, e[cnt].next = next[x], next[x] = cnt;
}
bool vis[N];
int fa[N];
int deep[N];
int size[N];
int pos[N],top[N];
int tim;
void dfs1(int x)
{
vis[x] = 1;
size[x] = 1;
deep[x] = deep[fa[x]] + 1;
for (int i = next[x]; i; i = e[i].next)
{
if (vis[e[i].to] || e[i].del)
continue;
e[i].use = e[i ^ 1].use = 1;
fa[e[i].to] = x;
dfs1(e[i].to);
size[x] += size[e[i].to];
}
}
void dfs2(int x,int chain)
{
vis[x] = 1;
pos[x] = ++tim;
top[x] = chain;
int k = 0;
for (int i = next[x]; i; i = e[i].next)
{
if (vis[e[i].to] || e[i].del)
continue;
if (size[e[i].to] > size[k])
k = e[i].to;
}
if (!k)
return;
dfs2(k,chain);
for (int i = next[x]; i; i = e[i].next)
{
if (vis[e[i].to] || e[i].del)
continue;
dfs2(e[i].to, e[i].to);
}
}
struct Tree
{
int s;
}t[N * 5];
void build(int d,int l,int r)
{
t[d].s = r - l + 1;
if (l == r)
return ;
int mid = (l + r) >> 1;
build(d * 2, l, mid);
build(d * 2 + 1, mid + 1, r);
}
void ins(int d,int l,int r,int x,int y)
{
if (!t[d].s || x > y)
return;
if (l == x && r == y)
{
t[d].s = 0;
return ;
}
int mid = (l + r) >> 1;
ins(d * 2, l, mid, x, min(y,mid));
ins(d * 2 + 1, mid + 1, r, max(mid + 1, x), y);
t[d].s = t[d * 2].s + t[d * 2 + 1].s;
}
int n,m;
int query(int d,int l,int r,int x,int y)
{
if (!t[d].s || x > y)
return 0;
if (l == x && r == y)
return t[d].s;
int mid = (l + r) >> 1;
return query(d * 2, l, mid, x, min(mid, y)) + query(d * 2 + 1, mid + 1, r, max(mid + 1, x), y);
}
void insT(int x,int y)
{
while (top[x] != top[y])
{
if (deep[top[x]] < deep[top[y]])
std::swap(x,y);
ins(1, 1, n, pos[top[x]], pos[x]);
x = fa[top[x]];
}
if (deep[x] < deep[y])
std::swap(x,y);
ins(1, 1, n, pos[y] + 1, pos[x]);
}
int queryT(int x,int y)
{
int ans = 0;
while (top[x] != top[y])
{
if (deep[top[x]] < deep[top[y]])
std::swap(x,y);
ans += query(1, 1, n, pos[top[x]], pos[x]);
x = fa[top[x]];
}
if (deep[x] < deep[y])
std::swap(x,y);
ans += query(1, 1, n, pos[y] + 1, pos[x]);
return ans;
}
struct Note
{
int x,y,op,ans;
}q[N * 2];
std::map<std::pair<int,int>,int> w;
int main()
{
n = read(), m = read();
cnt = 1;
for (int i = 1; i <= m; i++)
{
int x = read(), y = read();
if (x > y)
std::swap(x,y);
add(x,y), add(y,x);
w[mp(x,y)] = i;
}
int op = read(), tot = 0;
while (op != -1)
{
int x = read(), y = read();
if (x > y)
std::swap(x,y);
q[++tot].op = op; q[tot].x = x; q[tot].y = y;
if (!op)
{
int id = w[mp(x,y)];
e[id * 2 + 1].del = e[id * 2].del = 1;
}
op = read();
}
dfs1(1);
memset(vis, 0, sizeof(vis));
dfs2(1,1);
build(1,1,n);
for (int i = 2; i <= cnt; i+= 2)
if (!e[i].use && !e[i].del)
insT(e[i].to, e[i ^ 1].to);
for (int i = tot; i >= 1; i--)
if (!q[i].op)
insT(q[i].x, q[i].y);
else q[i].ans = queryT(q[i].x,q[i].y);
for (int i = 1; i <= tot; i++)
{
if (q[i].op == 1)
printf("%d\n",q[i].ans);
}
}