题目链接:Minimum spanning tree for each edge
题意:给一个无向图,n个点,m条边,对任意边edge[i],求出包含有边edge[i]的最小生成树。
做法:考虑MST的性质,对任意两点u,v一定有且只有一条路径,当边[u,v]不是MST边的时候,加入边[u,v]便会形成环,于是在u到v的路径上找一条权值最大的边删去再加入边[u,v],之前环上的任意两点仍然连通。
u到v路径的最大值用树链剖分维护,LCA大概也可以,不过做的时候顺便套了树剖的板子。
最开始求一次原始的MST,建立树剖后,就可以很方便地求出每个边的答案啦。
#include
#define ll long long int
using namespace std;
const int N = 200005;
struct eg{
int u, v, nex, val, id;
eg(){}
eg(int _u, int _v, int _val, int _nex) { u = _u, v = _v, val = _val, nex = _nex; }
bool operator < (eg a) const{
return val < a.val;
}
}edg[N<<2], MST[N];
ll ans[N] = {0};
int sav[N][3], scnt;
int fir[N], ecnt, root;
int n, m;
void add(int u, int v, int val){
edg[ecnt] = eg(u, v, val, fir[u]);
fir[u] = ecnt++;
edg[ecnt] = eg(v, u, val, fir[v]);
fir[v] = ecnt++;
}
////////////////////////////////////////////////////树链剖分
int fa[N], dep[N], siz[N], son[N];
int tree[N<<2];
void dfs(int rt){
siz[rt] = 1, son[rt] = 0;
for(int k = fir[rt]; k != -1; k = edg[k].nex){
if(edg[k].v != fa[rt]){
fa[edg[k].v] = rt;
dep[edg[k].v] = dep[rt] + 1;
dfs(edg[k].v);
if(siz[edg[k].v] > siz[son[rt]]) son[rt] = edg[k].v;
siz[rt] += siz[edg[k].v];
}
}
}
int w[N], top[N], cnt; //w[]为到线段树的映射
void dfs2(int rt, int tp){
w[rt] = ++cnt; top[rt] = tp;
if(son[rt]) dfs2(son[rt], top[rt]);
for(int k = fir[rt]; k != -1; k = edg[k].nex){
if(edg[k].v != son[rt] && edg[k].v != fa[rt]){
dfs2(edg[k].v, edg[k].v);
}
}
}
void update(int rt, int l, int r, int pos, int val){
if(l == r){
tree[rt] = val;
return;
}
int mid = (l+r) >> 1;
if(pos <= mid) update(rt<<1, l, mid, pos, val);
else update(rt<<1|1, mid+1, r, pos, val);
tree[rt] = max(tree[rt<<1], tree[rt<<1|1]);
}
void init(){
memset(siz, 0, sizeof(siz));
memset(tree, 0, sizeof(tree));
root = (n+1)/2;
fa[root] = cnt = dep[root] = 0;
dfs(root);
dfs2(root, root);
for(int i = 1; i <= scnt; ++i){
if(dep[sav[i][0]] > dep[sav[i][1]]) swap(sav[i][0], sav[i][1]);
update(1, 1, cnt, w[sav[i][1]], sav[i][2]);
}
}
int maxi(int rt, int l, int r, int ql, int qr){
if( ql > r || qr < l) return 0;
if(ql <= l && qr >= r) return tree[rt];
int mid = (l+r) >> 1;
return max(maxi(rt<<1, l, mid, ql, qr), maxi(rt<<1|1, mid+1, r, ql, qr));
}
int Treemax(int va, int vb){
int f1 = top[va], f2 = top[vb], tmp = 0;
while(f1 != f2){
if(dep[f1] < dep[f2]){
swap(f1, f2);
swap(va, vb);
}
tmp = max(tmp, maxi(1, 1, cnt, w[f1], w[va]));
va = fa[f1], f1 = top[va];
}
if(va == vb) return tmp;
if(dep[va] > dep[vb]) swap(va, vb);
return max(tmp, maxi(1, 1, cnt, w[son[va]], w[vb]));
}
///////////////////////////////////////////////////////
//////////////////////////////////////////////////////并查集
int seed[N];
int find(int x){
return seed[x] < 0? x : seed[x] = find(seed[x]);
}
int join(int a, int b){
a = find(a), b = find(b);
if(a == b) return 0;
if(seed[a] > seed[b]) seed[b] += seed[a], seed[a] = b;
else seed[a] += seed[b], seed[b] = a;
return 1;
}
/////////////////////////////////////////////////////
int main(){
memset(seed, -1, sizeof(seed));
memset(fir, -1, sizeof(fir));
ecnt = scnt = 0;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; ++i){
scanf("%d %d %d", &MST[i].u, &MST[i].v, &MST[i].val);
MST[i].id = i+1;
}
/////////////Kruskal求MST
sort(MST, MST+m);
for(int i = 0; i < m; ++i){
if(join(MST[i].u, MST[i].v)) {
ans[0] += MST[i].val;
//用MST里的边做树链剖分
add(MST[i].u, MST[i].v, MST[i].val);
sav[++scnt][0] = MST[i].u, sav[scnt][1] = MST[i].v, sav[scnt][2] = MST[i].val;
}
}
//////////////
//树链剖分go
init();
////////////
//然后就可以求解啦
for(int i = 0; i < m; ++i) ans[MST[i].id] = ans[0] + MST[i].val - Treemax(MST[i].u, MST[i].v);
for(int i = 1; i <= m; ++i) printf("%lld\n", ans[i]);
}