题目链接:http://codeforces.com/contest/609/problem/E

给你n个点,m条边。

问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少。

先求出最小生成树,树链剖分一下最小生成树。然后枚举m条边中的每条边,要是这条边是最小生成树的其中一边,则直接输出最小生成树的答案;否则就用树剖求u到v之间的最大边,然后最小生成树权值减去求出的最大边然后加上枚举的这条边就是答案。

 #include <bits/stdc++.h>
using namespace std;
typedef __int64 LL;
const int MAXN = 2e5 + ;
struct data {
int from , to , index;
bool flag;
LL cost;
bool operator <(const data& cmp) const {
return cost < cmp.cost;
}
}a[MAXN] , b[MAXN];
struct EDGE {
int next , to;
LL cost;
}edge[MAXN << ];
int head[MAXN] , tot;
int par[MAXN] , dep[MAXN] , son[MAXN] , size[MAXN];
int top[MAXN] , id[MAXN] , cnt; bool cmp(const data& a , const data &b) {
return a.index < b.index;
} void init(int n) {
memset(head , - , sizeof(head));
for(int i = ; i <= n ; ++i)
par[i] = i;
} int Find(int u) {
if(par[u] == u)
return u;
return par[u] = Find(par[u]);
} inline void add(int u , int v , LL cost) {
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].cost = cost;
head[u] = tot++;
} LL mst(int m) {
LL sum = ;
int f = ;
for(int i = ; i <= m ; ++i) {
int u = Find(a[i].from) , v = Find(a[i].to);
if(u != v) {
par[u] = v;
sum += a[i].cost;
a[i].flag = true;
add(a[i].from , a[i].to , a[i].cost);
add(a[i].to , a[i].from , a[i].cost);
++f;
b[f].from = a[i].from , b[f].to = a[i].to , b[f].cost = a[i].cost;
}
else {
a[i].flag = false;
}
}
return sum;
} void dfs1(int u , int p , int d) {
dep[u] = d , par[u] = p , son[u] = u , size[u] = ;
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
dfs1(v , u , d + );
if(size[v] >= size[son[u]])
son[u] = v;
size[u] += size[v];
}
} void dfs2(int u , int p , int t) {
top[u] = t , id[u] = ++cnt;
if(son[u] != u)
dfs2(son[u] , u , t);
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(v == son[u] || v == p)
continue;
dfs2(v , u , v);
}
} struct SegTree {
int l , r;
LL Max;
}T[MAXN << ]; void build(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r;
if(l == r) {
return ;
}
build(p << , l , mid);
build((p << )| , mid + , r);
} void updata(int p , int pos , LL num) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == T[p].r && T[p].l == pos) {
T[p].Max = num;
return ;
}
if(pos <= mid) {
updata(p << , pos , num);
}
else {
updata((p << )| , pos , num);
}
T[p].Max = max(T[p << ].Max , T[(p << )|].Max);
} LL query(int p , int l , int r) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
return T[p].Max;
}
if(r <= mid) {
return query(p << , l , r);
}
else if(l > mid) {
return query((p << )| , l , r);
}
else {
return max(query(p << , l , mid) , query((p << )| , mid + , r));
}
} LL find_max(int u , int v) {
int fu = top[u] , fv = top[v];
LL Max = ;
while(fv != fu) {
if(dep[fu] >= dep[fv]) {
Max = max(Max , query( , id[fu] , id[u]));
u = par[fu];
fu = top[u];
}
else {
Max = max(Max , query( , id[fv] , id[v]));
v = par[fv];
fv = top[v];
}
}
if(u == v)
return Max;
else if(dep[u] > dep[v])
return max(Max , query( , id[son[v]] , id[u]));
else
return max(Max , query( , id[son[u]] , id[v]));
} int main()
{
int n , m;
scanf("%d %d" , &n , &m);
if(m == ) {
printf("");
return ;
}
init(n);
for(int i = ; i <= m ; ++i) {
scanf("%d %d %lld" , &a[i].from , &a[i].to , &a[i].cost);
a[i].index = i;
}
sort(a + , a + m + );
LL mst_len = mst(m);
dfs1( , , );
dfs2( , , );
build( , , cnt);
for(int i = ; i <= n - ; ++i) {
if(dep[b[i].from] < dep[b[i].to])
swap(b[i].from , b[i].to);
updata( , id[b[i].from] , b[i].cost);
}
sort(a + , a + m + , cmp);
for(int i = ; i <= m ; ++i) {
if(a[i].flag)
printf("%lld\n" , mst_len);
else {
LL temp = find_max(a[i].from , a[i].to);
printf("%lld\n" , mst_len - temp + a[i].cost);
}
}
return ;
}

Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)的更多相关文章

  1. Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  2. Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST

    E. Minimum spanning tree for each edge   Connected undirected weighted graph without self-loops and ...

  3. CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种

    题目链接:http://codeforces.com/problemset/problem/609/E 大致就是有一棵树,对于每一条边,询问包含这条边,最小的一个生成树的权值. 做法就是先求一次最小生 ...

  4. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  5. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  6. CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  7. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

  8. codeforces 609E Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  9. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

随机推荐

  1. JAVA中获取工程路径的方法

    在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...

  2. uva 1642 Magical GCD

    很经典的题目,愣是没做出来.. 题意:给出一个序列,求一子序列,满足其GCD(子序列)* length(子序列)最大. 题解: 类似单调队列的思想,每次将前面所得的最大公约数与当前数进行GCD,若GC ...

  3. POJ 3253 Fence Repair【二叉堆】

    题意:给出n根木板,需要把它们连接起来,每一次连接的花费是他们的长度之和,问最少需要多少钱. 和上一题果子合并一样,只不过这一题用long long 学习的手写二叉堆的代码,再好好理解= = #inc ...

  4. 用户输入 i. 检测常用手势(一)

    参考: http://blog.csdn.net/qq418716640/article/details/8508973http://www.cnblogs.com/mengdd/p/3335508. ...

  5. UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)

    用新模板阿姨了一天,换成原来的一遍就ac了= = 题意很重要..最关键的一句话是说:若走A->B这条边,必然是d[B]<d[A],d[]数组保存的是各点到终点的最短路. 所以先做dij,由 ...

  6. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  7. 【C#学习笔记】播放wma/mp3文件

    using System; using System.Runtime.InteropServices; namespace ConsoleApplication { class Program { [ ...

  8. 《C++ Primer 4th》读书笔记 第4章-数组和指针

    原创文章,转载请注明出处: http://www.cnblogs.com/DayByDay/p/3911573.html

  9. Android 快速开发框架XUtils

    转载自:http://www.apkbus.com/forum.php?mod=viewthread&tid=241060&highlight=xUtils 最近搜了一些框架供初学者学 ...

  10. Ejabberd源码解析前奏--配置

    一.基本配置     配置文件将在你第一次启动ejabberd时加载,从该文件中获得的内容将被解析并存储到内部的ejabberd数据库中,以后的配置将从数据库加载,并且任何配置文件里的命令都会被添加到 ...