BZOJ 1969: [Ahoi2005]LANE 航线规划 [树链剖分 时间倒流]

时间:2022-10-07 16:12:43

题意:

一张图,删除边,求两点之间的割边数量。保证任意时刻图连通


任求一棵生成树,只有树边可能是割边

时间倒流,加入一条边,就是两点路径上的边都不可能是割边,区间覆盖...

然后本题需要把边哈希一下,手写哈希比map快很多

貌似还有一种不用树剖的做法,不管了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+, M=1e5+, P=;
#define lc x<<1
#define rc x<<1|1
#define mid ((l+r)>>1)
#define lson lc, l, mid
#define rson rc, mid+1, r
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int n, m, trn, mark[N], Q, c, x, y, del[M];
int Hash[P];
inline int& Map(int x) {return Hash[x%P];}
struct meow{int u, v;} a[M], tr[N];
struct qmeow{int c, u, v, qid;}q[M];
int ans[M]; struct Graph{
struct edge{int v, ne, id;} e[M<<];
int cnt, h[N];
inline void ins(int u, int v, int id) {
e[++cnt]=(edge){v, h[u], id}; h[u]=cnt;
e[++cnt]=(edge){u, h[v], id}; h[v]=cnt;
}
int vis[N];
void dfs(int u) {
vis[u]=;
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v]) {
mark[e[i].id]=;
tr[++trn]=a[e[i].id];
dfs(e[i].v);
}
}
}G; struct edge{int v, ne;} e[N<<];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt]=(edge){v, h[u]}; h[u]=cnt;
e[++cnt]=(edge){u, h[v]}; h[v]=cnt;
}
int dfn[N], dfc, fa[N], top[N], deep[N], mx[N], size[N];
void dfs(int u) {
size[u]=;
for(int i=h[u];i;i=e[i].ne) {
int v=e[i].v;
if(v==fa[u]) continue;
deep[v]=deep[u]+; fa[v]=u;
dfs(v);
size[u]+=size[v];
if(size[v]>size[mx[u]]) mx[u]=v;
}
}
void dfs2(int u, int anc) { //printf("u anc %d %d\n",u,anc);
dfn[u] = ++dfc; top[u] = anc;
if(mx[u]) dfs2(mx[u], anc);
for(int i=h[u];i;i=e[i].ne)
if(e[i].v != fa[u] && e[i].v != mx[u]) dfs2(e[i].v, e[i].v);
} struct SegmentTree{
struct meow{
int sum, tag;
meow():tag(-){}
}t[N<<];
inline void paint(int x, int l, int r, int v) {
t[x].tag = v;
t[x].sum = (r-l+)*v;
}
inline void pushDown(int x, int l, int r) {
if(t[x].tag != -) {
paint(lson, t[x].tag);
paint(rson, t[x].tag);
t[x].tag = ;
}
}
void build(int x, int l, int r) {
if(l==r) t[x].sum=;
else {
build(lson); build(rson);
t[x].sum = t[lc].sum + t[rc].sum;
}
}
inline void cover(int x, int l, int r, int ql, int qr, int v) {
if(ql<=l && r<=qr) paint(x, l, r, v);
else {
pushDown(x, l, r);
if(ql<=mid) cover(lson, ql, qr, v);
if(mid<qr) cover(rson, ql, qr, v);
t[x].sum = t[lc].sum + t[rc].sum;
}
}
inline int que(int x, int l, int r, int ql, int qr) {
if(ql<=l && r<=qr) return t[x].sum;
else {
pushDown(x, l, r);
int ans=;
if(ql<=mid) ans+=que(lson, ql, qr);
if(mid<qr) ans+=que(rson, ql, qr);
return ans;
}
}
}S; void cover(int x, int y, int v) {
while(top[x] != top[y]) {
if(deep[top[x]] < deep[top[y]]) swap(x, y);
S.cover(,,n,dfn[top[x]],dfn[x],v);
x = fa[top[x]];
}
if(dfn[x] > dfn[y]) swap(x, y);
if(x!=y) S.cover(,,n,dfn[x]+,dfn[y],v);
}
int query(int x, int y) {
int ans=;
while(top[x] != top[y]) {
if(deep[top[x]] < deep[top[y]]) swap(x, y);
ans += S.que(,,n,dfn[top[x]],dfn[x]);
x = fa[top[x]];
}
if(dfn[x] > dfn[y]) swap(x, y);
if(x!=y) ans += S.que(,,n,dfn[x]+,dfn[y]);
return ans;
} int main() {
//freopen("in","r",stdin);
n=read(); m=read();
for(int i=; i<=m; i++) {
x=read(); y=read(); if(x>y) swap(x, y);
a[i]=(meow){x, y};
Map(x*n+y)=i;
}
while(true) {
c=read();
if(c==-) break;
x=read(); y=read(); if(x>y) swap(x, y);
q[++Q]=(qmeow){c, x, y, };
if(c==) del[ Map(x*n+y) ] = ;
else q[Q].qid = ++ans[];
}
for(int i=; i<=m; i++) if(!del[i]) G.ins(a[i].u, a[i].v, i); //printf("hi %d\n",i); G.dfs();
for(int i=; i<=trn; i++) ins(tr[i].u, tr[i].v);// printf("tr %d %d\n",tr[i].u, tr[i].v);
dfs(); dfs2(, );
S.build(, , n);
for(int i=; i<=m; i++) if(!mark[i] && !del[i]) cover(a[i].u, a[i].v, ); for(int i=Q; i>=; i--) { //printf("Q %d\n",i);
if(q[i].c==) cover(q[i].u, q[i].v, );
else ans[q[i].qid] = query(q[i].u, q[i].v);
}
for(int i=; i<=ans[]; i++) printf("%d\n",ans[i]);
}