# [树链剖分+MST] CF609E. Minimum spanning tree for each edge

1171人阅读 评论(0)

u到v路径的最大值用树链剖分维护，LCA大概也可以，不过做的时候顺便套了树剖的板子。

#include<bits/stdc++.h>
#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里的边做树链剖分
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]);
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：31809次
• 积分：1056
• 等级：
• 排名：千里之外
• 原创：77篇
• 转载：0篇
• 译文：0篇
• 评论：9条
文章分类
最新评论