Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)
题目链接: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 (最小生成树+树链剖分)的更多相关文章
- 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 ...
- 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 ...
- CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种
题目链接:http://codeforces.com/problemset/problem/609/E 大致就是有一棵树,对于每一条边,询问包含这条边,最小的一个生成树的权值. 做法就是先求一次最小生 ...
- 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 ...
- 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 ...
- 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 ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- 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 ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
随机推荐
- struts.custom.i18n.resources 如何配置多个资源文件?
struts.custom.i18n.resources = resources1,resources2,resources3 配置properties文件
- Python3 学习第二弹: 字符串String
字符串表示问题 常见用法 '' 与 "" 就不提了 一些特殊用法 三引号:接收多行字符串的输入 >>>print('''Oh my God!''') Oh my ...
- bzoj3261: 最大异或和
可持久化trie.又是%%%Xs酱... #include<cstdio> #include<cstring> #include<iostream> #includ ...
- css3的背景多重运用
效果图: 简单代码: http://www.developerdrive.com/2013/08/introducing-css3-multiple-backgrounds/ 演示地址: http:/ ...
- main cannot be resolved or is not a field
今天在做XML解析的时候,总是给我报 XML Parsing Error: XML or text declaration not at start of entity 的错误,后来查了下讲大概意思是 ...
- UVALive 4452 The Ministers' Major Mess(2-sat)
2-sat.又学到了一种使用的方法:当确定选择某中状态A时,从它的对立状态A^1引一条边add(A^1,A),从而使凡是dfs经过对立状态,必然return false:即保证若存在一种可能性,必然是 ...
- Android adb install INSTALL_FAILED_DEXOPT
说明: 之前一直认为将eclipse的Android项目直接cp到Android源码下编译就行了,实际情况是还要注意其他的文件架构. 错误现象: c:\Users\zengjf>adb inst ...
- RPi 2B Documentation
/********************************************************************** * RPi 2B Documentation * 声明: ...
- 聚焦 SQL 数据库活动异地复制
Tobias Ternstrom US-DS-PM 首席部门项目经理 本文作为一系列业务连续性和灾难恢复文章的开篇,概述了业务连续性的各种场景,然后重点介绍 SQL 数据库高级服务级别提供的活动异地 ...
- 【再见RMQ】NYOJ-119-士兵杀敌(三),区间内大小差值
[题目链接:NYOJ-119] 思路:转自 点我 ,讲的挺好. #include <cstdio> #include <math.h> #define max(a,b) ((a ...