问题描述
支持,加边,删边或者修改边权。询问图每个时刻的最大生成树或者联通块个数,两点路径上最小边权的最大值
Problem 1: 加边,删边,求桥的个数
先cdq分治修改,
把每一层不被修改涉及的所有边加入图中,tarjian缩边双,然后建树。
把这棵树上的非关键点压缩掉。建出新的虚树。
这样每一层的树的大小和边数都是O(区间长度)
总复杂度O(nlogn)
注意:
图有两次重标号。
cdq的时候边是否应该在当前区间存在要讨论
建虚树的时候可能有非常多的单点,因此不能直接再假设根也是关键点。
把树压缩后,边权要累加。答案边压缩边统计
如果用vector存边空间会炸(其实这样做空间是线性的,但是vector无法释放内存)。下面的代码本机过了对拍,但是仍然处于MLE的状态
总结:
我太缺乏把复杂的对应关系和算法转化成代码的能力。开始的时候完全不知道应该怎么写。写了2个多小时才写完。
然后调试的过程没有给自己定时。
开始对着代码检查,写注释,后来依靠输出调试和对拍。
必须给自己定时,过了时间就不能调了。浪费了3个多小时在调这道题上(其中30分钟还在卡空间,根本没有太多意义)
开始的时候总是思维清晰,这样的调试是必须并且有意义的。但是当自己已经混乱,就不能再往下浪费时间了!要能够管住自己,该停就需要停下来!
算法转化为代码的能力需要提高,如何精确的描绘出算法,用STL,语法优化自己的代码结构,这个需要一直总结!
#include<bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(register int i = l ; i <= r ; i++)
#define repd(i,r,l) for(register int i = r ; i >= l ; i--)
#define rvc(i,S) for(register int i = 0 ; i < (int)S.size() ; i++)
#define rvcd(i,S) for(register int i = ((int)S.size()) - 1 ; i >= 0 ; i--)
#define fore(i,x)for (register int i = head[x] ; i ; i = e[i].next)
#define forup(i,l,r) for (register int i = l ; i <= r ; i += lowbit(i))
#define fordown(i,id) for (register int i = id ; i ; i -= lowbit(i))
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define lowbit(x) ((x)&(-(x)))
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pr;
const int maxn = 100020;
const int inf = 1e9;
struct node{
int next,to,w;
};
struct edge{
int u,v,w,id;
};
vector <edge> E[maxn * 4],tmp;
struct Query{
int id,tp;
}q[maxn];
int n,m,K,edge_cnt;
map <int,int> edge_id[maxn];
int res[maxn];
int tag[maxn],tag2[maxn],c[maxn],fa[maxn],col[maxn],key[maxn],id[maxn],vis[maxn];
namespace Tree{
node e[maxn * 2];
int head[maxn],cnt;
int tot,tag[maxn];
void clear(int n){
rep(i,1,n) head[i] = tag[i] = 0;
cnt = tot = 0;
}
inline void adde(int x,int y,int w){
e[++cnt].to = y;
e[cnt].next = head[x];
e[cnt].w = w;
head[x] = cnt;
}
int dfs(int x,int fa){ //标记在虚树中的点
int sz = 0;
tag[x] = 0 , vis[x] = 1;
fore(i,x){
if ( e[i].to == fa ) continue;
sz += dfs(e[i].to,x);
}
if ( key[x] || sz >= 2 ) tag[x] = 1;
return tag[x];
}
void dfs2(int x,int fa,int last,int len){ //压缩树,把下一层的有用边得到
if ( tag[x] ){
++tot;
if ( last ) tmp.pb((edge){last,tot,len,0}); //压缩后的边id = 0,和修改边区分
last = id[x] = tot , len = 0;
}
else id[x] = last;
fore(i,x){
if ( e[i].to == fa ) continue;
dfs2(e[i].to,x,last,len + e[i].w);
}
}
}
namespace graph{
int dfn[maxn],low[maxn],dfstime,tops,belong[maxn],bcccnt;
int st[maxn];
node e[maxn * 2];
int head[maxn],cnt;
inline void adde(int x,int y,int w){
e[++cnt].to = y;
e[cnt].next = head[x];
e[cnt].w = w;
head[x] = cnt;
}
void clear(int tot){
rep(i,1,tot) head[i] = dfn[i] = low[i] = belong[i] = 0;
cnt = 1 , dfstime = tops = bcccnt = 0;
}
void tarjian(int x,int fa){
low[x] = dfn[x] = ++dfstime;
st[++tops] = x;
fore(i,x){
if ( (i ^ 1) == fa ) continue;
if ( !dfn[e[i].to] ){
tarjian(e[i].to,i);
if ( dfn[e[i].to] == low[e[i].to] ){
++bcccnt;
while ( tops && st[tops] != x ){
int cur = st[tops--];
belong[cur] = bcccnt;
key[bcccnt] |= col[cur];
}
}
else low[x] = min(low[x],low[e[i].to]);
}
else low[x] = min(low[x],dfn[e[i].to]);
}
}
void rebuild(int &tot,int &ans){
rep(i,1,tot) if ( !dfn[i] ){
tarjian(i,0);
if (tops){ //i点所在的边双
bcccnt++;
while (tops){
int cur = st[tops--];
belong[cur] = bcccnt;
key[bcccnt] |= col[cur];
}
}
}
// cout<<"tarjian "<<tot<<" "<<bcccnt<<"\n";
rep(x,1,tot){
int u = belong[x];
fore(i,x){
if ( belong[e[i].to] != u ){
Tree::adde(u,belong[e[i].to],e[i].w);
// cout<<u<<" "<<belong[e[i].to]<<" "<<e[i].w<<endl;
}
else if ( e[i].to > x ) ans -= e[i].w; //肯定不是桥的边直接在答案中更新
}
}
// rep(i,1,bcccnt) cout<<key[i]<<" ";
// cout<<endl;
// rep(i,1,tot) cout<<belong[i]<<" ";
// cout<<endl;
tot = bcccnt;
}
}
void clear(int tot){
tmp.clear();
rep(i,1,tot) col[i] = vis[i] = key[i] = 0;
Tree::clear(tot);
graph::clear(tot);
}
void compress(int &tot){
rep(i,1,tot){
if ( !vis[i] ){
// key[i] = 1;
Tree::dfs(i,0);
Tree::dfs2(i,0,0,0);
}
}
// cout<<"compress "<<Tree::tot<<endl;
// rep(i,1,tot) cout<<id[i]<<" ";
// cout<<endl;
tot = Tree::tot;
}
void CDQ(int x,int l,int r,int ans,int tot){
int mid = (l + r) >> 1 , ls = x << 1 , rs = (x << 1) | 1;
if ( l < r || q[l].tp == -1 ){
rep(i,l,r){ //把所有区间内涉及到的修改边打上标记
if ( !tag[q[i].id] ) tag[q[i].id] = i; //标记为最前面一次修改。维护加边、删边顺序
}
repd(i,r,l){
if ( !tag2[q[i].id] ) tag2[q[i].id] = i;
}
}
clear(tot); //进行所有清空
// cout<<" current layer "<<l<<" "<<r<<" ans "<<ans<<" "<<tot<<endl;
rvc(i,E[x]){
// cout<<E[x][i].u<<" "<<E[x][i].v<<" "<<" "<<E[x][i].w<<" "<<E[x][i].id<<endl;
}
// cout<<" edge \n";
rvc(i,E[x]){ //加入不修改的边
if ( !tag[E[x][i].id] ){
if ( E[x][i].u == E[x][i].v ) continue;
graph::adde(E[x][i].u,E[x][i].v,E[x][i].w);
graph::adde(E[x][i].v,E[x][i].u,E[x][i].w);
// cout<<E[x][i].u<<" "<<E[x][i].v<<endl;
if ( E[x][i].id ) ans++; //如果是操作加班,桥的个数+1
}
else{
col[E[x][i].u] = col[E[x][i].v] = 1; //标记被修改的点
}
}
// cout<<"debuff "<<ans<<endl;
graph::rebuild(tot,ans); //缩边双,重建图
if ( l == r ){
res[l] = ans;
rep(i,l,r) tag2[q[i].id] = tag[q[i].id] = 0; //清空tag标记
return;
}
compress(tot); //把树压缩,删去与关键点无关的点
rvc(i,tmp) E[ls].pb(tmp[i]) , E[rs].pb(tmp[i]);
rvc(i,E[x]){
int t = tag[E[x][i].id],t2 = tag2[E[x][i].id];
if ( t ){
int u = id[graph::belong[E[x][i].u]] , v = id[graph::belong[E[x][i].v]]; //当前的修改边,对应点重标号后加入下一层
// if ( u == v ) 修改的边,即使是自环。先传到后面在考虑
if ( t2 > mid || q[t2].tp == 1 ) E[rs].pb((edge){u,v,E[x][i].w,E[x][i].id});
if ( t <= mid ||q[t].tp == -1 ) E[ls].pb((edge){u,v,E[x][i].w,E[x][i].id});
}
}
rep(i,l,r) tag2[q[i].id] = tag[q[i].id] = 0; //清空tag标记
// E[x].clear() , E[x].resize(0); //节省空间
vector <edge>().swap(E[x]);
CDQ(ls,l,mid,ans,tot);
CDQ(rs,mid + 1,r,ans,tot);
}
int main(){
// freopen("input.txt","r",stdin);
scanf("%d %d",&n,&K);
rep(i,1,K){
char ch[20]; int x,y;
scanf("%s%d%d",ch,&x,&y);
if ( x > y ) swap(x,y);
int &id = edge_id[x][y];
if ( !id ) id = ++edge_cnt;
if ( ch[0] == 'A' ){
q[i].id = id , q[i].tp = 1;
}
else{
q[i].id = id , q[i].tp = -1;
}
}
rep(i,1,n){
for ( auto it : edge_id[i] )
E[1].pb((edge){i,it.fi,1,it.se});
}
CDQ(1,1,K,0,n);
rep(i,1,K) printf("%d\n",res[i]);
}
problem2:修改边权求最小生成树
这题只需要记录边权,所以可以直接用并查集压缩,注意点的重标号即可
如果要考虑最小生成树上两点路径的有关性质,可以在压缩的时候同样用上题建虚树,缩掉无关的点的思路,维护出上一层的最小生成虚树。但是实现起来也非常麻烦。
这个题在赛场上遇到过一次,不过点数非常少,直接暴力建生成树即可,不用cdq。如果再遇到类似的应该仔细想想实现的细节,不能草率的去写!