Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树
2 seconds
256 megabytes
standard input
standard output
Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges.
For each edge (u, v) find the minimal possible weight of the spanning tree that contains the edge (u, v).
The weight of the spanning tree is the sum of weights of all edges included in spanning tree.
First line contains two integers n and m (1 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105) — the number of vertices and edges in graph.
Each of the next m lines contains three integers ui, vi, wi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 109) — the endpoints of the i-th edge and its weight.
Print m lines. i-th line should contain the minimal possible weight of the spanning tree that contains i-th edge.
The edges are numbered from 1 to m in order of their appearing in input.
5 7
1 2 3
1 3 1
1 4 5
2 3 2
2 5 3
3 4 2
4 5 4
9
8
11
8
8
8
9
题意:给你n个点,m条边;
第i条为必选边,求最小的生成树;
思路:先建好一颗最小生成树,如果边在生成树上,输出最小的即可;
对于不在树上的如何求解:
原来建好的一棵树,再加入一条边,会使得形成一个环,去查找原来最小生成树中u到v最大的边权,最小生成树的权值减去最大的边权+当前的边权即使答案;
无更新的区间最大值,可以用倍增的写法;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=; ///数组大小
struct edge
{
int v,next;
} edge[N<<];
int head[N<<],edg,id,n;
/// 树链剖分 int fa[N],dep[N],son[N],siz[N]; // fa父亲,dep深度,son重儿子,siz以该点为子树的节点个数
int ran[N],top[N],tid[N],num[N]; // tid表示边的标号,top通过重边可以到达最上面的点,ran表示标记tid
void init()
{
memset(son,-,sizeof(son));
memset(head,-,sizeof(head));
edg=;
id=;
} void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].next=head[u];
head[u]=edg;
} void dfs1(int u,int fath,int deep)
{
fa[u]=fath;
siz[u]=;
dep[u]=deep;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==fath)continue;
dfs1(v,u,deep+);
siz[u]+=siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u]=v;
}
} void dfs2(int u,int tp)
{
tid[u]=++id;
top[u]=tp;
ran[tid[u]]=u;
if(son[u]==-)return;
dfs2(son[u],tp);
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==fa[u])continue;
if(v!=son[u])
dfs2(v,v);
}
} struct SGT
{
int maxx[N<<];
void pushup(int pos)
{
maxx[pos]=max(maxx[pos<<],maxx[pos<<|]);
}
void build(int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=num[ran[l]];
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
int query(int L,int R,int l,int r,int pos)
{
//cout<<L<<" "<<R<<" "<<l<<" "<<r<<endl;
if(L<=l&&r<=R)return maxx[pos];
int mid=(l+r)>>;
int ans=;
if(L<=mid)ans=max(ans,query(L,R,l,mid,pos<<));
if(R>mid) ans=max(ans,query(L,R,mid+,r,pos<<|));
return ans;
}
}tree; int up(int l,int r)
{
int ans=;
while(top[l]!=top[r])
{
if(dep[top[l]]<dep[top[r]])swap(l,r); ans=max(ans,tree.query(tid[top[l]],tid[l],,n,));
l=fa[top[l]];
} if(dep[l]<dep[r])swap(l,r);
if(l==r)return ans;
ans=max(ans,tree.query(tid[son[r]],tid[l],,n,));
return ans;
}
/// 克鲁斯卡尔
struct is
{
int u,v,w,pos;
operator <(const is &x)const
{
return w<x.w;
}
}a[N];
int fafa[N],ans[N];
int Find(int x)
{
return x==fafa[x]?x:fafa[x]=Find(fafa[x]);
}
ll out[N];
int main()
{
init();
int m;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w),a[i].pos=i;
sort(a+,a++m);
ll minn=;
for(int i=;i<=n;i++)
fafa[i]=i;
for(int i=;i<=m;i++)
{
int x=Find(a[i].u);
int y=Find(a[i].v);
if(x!=y)
{
add(a[i].u,a[i].v);
add(a[i].v,a[i].u);
fafa[x]=y;
minn+=a[i].w;
ans[i]=;
}
}
dfs1(,-,);
dfs2(,);
for(int i=;i<=m;i++)
{
if(ans[i])
{
if(fa[a[i].u]==a[i].v)
num[a[i].u]=a[i].w;
else
num[a[i].v]=a[i].w;
}
}
tree.build(,n,);
for(int i=;i<=m;i++)
{
if(ans[i])out[a[i].pos]=minn;
else
{
int x=up(a[i].u,a[i].v);
out[a[i].pos]=minn-x+a[i].w;
}
}
for(int i=;i<=m;i++)
printf("%lld\n",out[i]);
return ;
}
Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树的更多相关文章
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)
题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...
- 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 ...
- 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
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
随机推荐
- linux系统Centos环境下搭建SVN服务器及权限配置
linux系统Centos环境下如何搭建SVN服务器以及svnserve.conf.authz.passwd配置文件详细介绍 至于svn的概念,这里就不做详细阐述了,可以自行百度.简单来讲就是一个 ...
- JS 和 Jquery 的一些常用效果
https://www.cnblogs.com/beiz/tag/%E7%BD%91%E9%A1%B5%E5%B8%B8%E8%A7%81%E6%95%88%E6%9E%9C/ 北执
- c#md5加密的简单用法
using System.Security.Cryptography; //MD5 md5 = MD5.Create(); MD5 md5 = new MD5CryptoServiceProvider ...
- 计算概论(A)/基础编程练习(数据成分)/3:整数的个数
#include<stdio.h> int main() { ] = {}; // 输入k个正整数 scanf("%d",&k); // 循环读入和进行算术 w ...
- Source Insight 如何将script等文件加入
点击菜单栏Options -> Document Options , 然后再弹出的对话框中找到File filter,文件过滤的设置,里面肯能有*.c;*.h 你在后面添加Makefile,注意 ...
- 判断PC或mobile设备
js 限制: <script type="text/javascript"> function uaredirect(f){try{if(document.getEle ...
- Django 事物
事物 在这里指,将一些关于数据库的一系列操作,打包成一个原子性操作,意思是这一系列操作必须全部执行成功,如果,其中某个操作没有成功,那么这一系列操作都将滚回到之前没执行的状态,包括其中执行成功的某些操 ...
- Linux模拟僵尸进程并kill
模拟系统有僵尸进程后怎么解决 僵尸进程 #include <stdio.h> #include <sys/types.h> int main() { //fork a chil ...
- Java基础语法(上)
Java编译报错出现非法字符,原因是存在中文字符. Java关键字的字母都是小写. Java是一种强类型语言,针对每一种数据都给出了明确的数据类型. 数据类型分类: A:基本数据类型 B:引用数据类型 ...
- Windows 登录用户的类型
转载:http://blog.csdn.net/chenlycly/article/details/45419259 1.登录的是超级管理员Administrator 默认情况下,超级管理员Admin ...