CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种
题目链接:http://codeforces.com/problemset/problem/609/E
大致就是有一棵树,对于每一条边,询问包含这条边,最小的一个生成树的权值。
做法就是先求一次最小生成树,标记最小生成树上的边,对于这些边,直接就是原始最小生成树。否则必然可以在去掉u到v路径上最长边,再加上边u->v,这一定是包含此边最小的生成树。
查询最长边,可以用树链剖分,也可以树上倍增。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#include <cassert> using namespace std; const int N=2e5+;
const int INF=0x3f3f3f3f;
struct Node {
int u,v,w;
int id;
bool operator < (const Node &o) const {
return w<o.w;
}
}node[N];
struct Edge{
int to,next,w;
}edge[N<<];
int idx,head[N];
void addedge(int u,int v,int w){
++idx;
edge[idx].to=v;
edge[idx].next=head[u];
edge[idx].w=w;
head[u]=idx;
}
int dep[N];
int par[N][];
int dis[N][];
void dfs(int u,int f,int d){
dep[u]=d;
for (int k=head[u];k;k=edge[k].next){
int v=edge[k].to;
if (v==f) continue;
par[v][]=u;
dis[v][]=edge[k].w;
dfs(v,u,d+);
}
}
int kthA(int u,int k) {
for (int i=;i>=;i--) {
if (k>=(<<i)) {
k-=(<<i);
u=par[u][i];
}
}
return u;
}
int kthD(int u,int k) {
int ret=;
for (int i=;i>=;i--) {
if (k>=(<<i)) {
k-=(<<i);
ret=max(ret,dis[u][i]);
u=par[u][i];
}
}
return ret;
}
void calc(int n) {
for (int i=;i<=;i++) {
int k=<<(i-);
for (int j=;j<=n;j++) {
par[j][i]=par[par[j][i-]][i-];
dis[j][i]=max(dis[j][i-],dis[par[j][i-]][i-]);
}
}
}
int get(int u,int v){
if(dep[u]<dep[v])swap(u,v);
int ret=kthD(u,dep[u]-dep[v]);
u=kthA(u,dep[u]-dep[v]);
if(u==v)return ret;
for(int i=;i>=;i--){
if(par[u][i]==par[v][i])continue;
ret=max(ret,dis[u][i]);
ret=max(ret,dis[v][i]);
u=par[u][i];
v=par[v][i];
}
ret=max(ret,dis[u][]);
ret=max(ret,dis[v][]);
return ret;
}
int fa[N];
int find(int x) {
if (fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
bool mark[N];
long long ret[N];
int main () {
ios_base::sync_with_stdio(false);
int n,m;
while (cin>>n>>m) {
for (int i=;i<=m;i++) {
cin>>node[i].u>>node[i].v>>node[i].w;
node[i].id=i;
mark[i]=false;
}
sort(node+,node++m);
long long sum=;
for (int i=;i<=n;i++)
fa[i]=i;
idx=;memset(head,,sizeof head);
for (int i=;i<=m;i++) {
int u=node[i].u;
int v=node[i].v;
int w=node[i].w;
int id=node[i].id;
int fu=find(u);
int fv=find(v);
if (fu==fv) continue;
fa[fu]=fv;
sum+=w;
mark[id]=true;
addedge(u,v,w);
addedge(v,u,w);
}
dfs(,-,);
calc(n);
for (int i=;i<=m;i++) {
int id=node[i].id;
int u=node[i].u;
int v=node[i].v;
int w=node[i].w;
if (mark[id]) ret[id]=sum;
else {
int mx=get(u,v);
ret[id]=sum+w-mx;
}
}
for (int i=;i<=m;i++)
cout<<ret[i]<<endl;
}
return ;
}
CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种的更多相关文章
- 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 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 (最小生成树+树链剖分)
题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...
- 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 ...
- 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 ...
- [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 ...
- Codeforces Edu3 E. Minimum spanning tree for each edge
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- 关于使用mybatis中mapper instrances,通过session另一种操作方式
String resource = "mybatis-config.xml"; InputStream inputStream = null; try { // 获取SqlSess ...
- Vue学习之路---No.6(分享心得,欢迎批评指正)
我们还是先回顾一下上一次的重点: 1.事件绑定,我们可以对分别用方法和js表达式对事件进行处理 2.当方法名带括号的时候,在方法中一定要传参:而不带括号的时候,vm会自动配置默认event 3.各类事 ...
- 通过Map间接比较两个Json格式是否相同
首先,我们举个例子来对两个Json格式进行比较 第一个Json格式: {"singleway":[],"multiway":{"channelSlav ...
- Bootsrap 的 Carousel
一.简介 Carousel 就是指轮播图,这里 有完整的代码例子.它可以很简单的就构造出来,结构如下: div.carousel.slide[data-ride="carousel" ...
- 谷歌开源图片压缩算法Guetzli实测体验报告
谷歌大神又出开源新技术啦,这次是对JPEG格式的图片采用全新算法重新编码,输出的图片还是JPEG但是图片大小明显缩小,而质量不但没有损失,甚至还更加优化,速速来体验一把. 一.环境安装 下载谷歌开源软 ...
- Exchange无法发送邮件 未找到匹配的连接器来路由外部收件人解决办法
使用命令行管理程序创建发送连接器 本示例创建发送连接器,用于集线器传输服务器 HubA 向 Internet 发送电子邮件. 复制 New-SendConnector -Name "In ...
- Android两种为ViewPager+Fragment添加Tab的方式
在Android开发中ViewPager的使用是非常广泛的,而它不仅仅能够实现简单的开始引导页,还可以结合Fragment并添加Tab作为选项卡或为显示大批量页面实现强大的顺畅滑动 下面介绍两种为Vi ...
- 新手向:Java基础知识
Java 接口 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并 ...
- Webstorm编译TypeScript报错
Accessors are only available when targeting ECMAscript 5 and higher. 解决办法: File Watchers 在tsc.cmd命令上 ...
- nginx浏览目录
[root@localhost domains]# vi web.jd.com location / proxy_set_header X-Forwarded-For $proxy_add_x_for ...