cf 609E.Minimum spanning tree for each edge
最小生成树,lca(树链剖分(太难搞,不会写))
问存在这条边的最小生成树,2种情况。1.这条边在原始最小生成树上。2.加上这条半形成一个环(加上),那么就找原来这条边2端点间的最大边就好(减去)。(sum+val-max)
(代码冗长)
#include<bits/stdc++.h>
#define LL long long
#define N 100005
using namespace std;
inline int ra()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
struct node{
int x,y,id,v;
}a[N<<];
struct data{
int to,next,v;
}e[N<<];
int n,m,deep[N<<],fa[N<<][];
int father[N<<],cnt;
LL sum;
int mx[N<<][],head[N<<];
bool vis[N<<];
void insert(int x, int y, int v)
{
e[++cnt].next=head[x];
e[cnt].to=y;
e[cnt].v=v;
head[x]=cnt;
}
int find(int x)
{
return father[x]==x?x:father[x]=find(father[x]);
}
bool cmp(node a, node b)
{
return a.v<b.v;
}
bool cmpid(node a, node b)
{
return a.id<b.id;
}
void dfs(int x, int f)
{
for (int i=; i<=; i++)
{
fa[x][i]=fa[fa[x][i-]][i-];
mx[x][i]=max(mx[x][i-],mx[fa[x][i-]][i-]);
}
for (int i=head[x];i;i=e[i].next)
{
if (e[i].to==f) continue;
fa[e[i].to][]=x;
mx[e[i].to][]=e[i].v;
deep[e[i].to]=deep[x]+;
dfs(e[i].to,x);
}
}
int getans(int x, int y)
{
int ans=;
if (deep[x]<deep[y]) swap(x,y);
int t=deep[x]-deep[y];
for (int i=; i<=; i++)
if (t&(<<i)) ans=max(ans,mx[x][i]),x=fa[x][i];
for (int i=; i>=; i--)
if (fa[x][i]!=fa[y][i])
{
ans=max(ans,max(mx[x][i],mx[y][i]));
x=fa[x][i]; y=fa[y][i];
}
if (x!=y) return max(ans,max(mx[x][],mx[y][]));
return ans;
}
int main()
{
n=ra(); m=ra();
for (int i=; i<=m; i++)
{
int x=ra(),y=ra(),v=ra();
a[i].x=x; a[i].y=y; a[i].v=v; a[i].id=i;
}
sort(a+,a+m+,cmp); int tot=;
for (int i=; i<=n; i++) father[i]=i;
for (int i=; i<=m; i++)
{
int q=find(a[i].x),p=find(a[i].y);
if (p!=q)
{
vis[a[i].id]=;
father[p]=q;
sum+=a[i].v;
tot++;
insert(a[i].x,a[i].y,a[i].v);
insert(a[i].y,a[i].x,a[i].v);
// cout<<a[i].x<<" "<<a[i].y<<" "<<a[i].v<<endl;
}
if (tot==n-) break;
}
dfs(,);
sort(a+,a+m+,cmpid);
for (int i=; i<=m; i++)
{
if (vis[i]) printf("%I64d\n",sum);
else {
cout<<sum+(LL)a[i].v-(LL)getans(a[i].x,a[i].y)<<endl;
// cout<<getans(a[i].x,a[i].y);while (1);
}
}
return ;
}
cf 609E.Minimum spanning tree for each edge的更多相关文章
- [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 ...
- Educational Codeforces Round 3 E (609E) Minimum spanning tree for each edge
题意:一个无向图联通中,求包含每条边的最小生成树的值(无自环,无重边) 分析:求出这个图的最小生成树,用最小生成树上的边建图 对于每条边,不外乎两种情况 1:该边就是最小生成树上的边,那么答案显然 2 ...
- codeforces 609E. Minimum spanning tree for each edge 树链剖分
题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...
- 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 LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- LCS(Longest Common Subsequence)最长公共子序列
最长公共子序列(LCS)是一个在一个序列集合中(通常为两个序列)用来查找所有序列中最长子序列的问题.这与查找最长公共子串的问题不同的地方是:子序列不需要在原序列中占用连续的位置 .最长公共子序列问题是 ...
- wdcp升级php5.8到php7.1.12后安装禅道
()下载禅道安装包 http://www.zentao.net/download/zentao10.0.beta-80076.html ()安装禅道 http://www.zentao.net/boo ...
- MyEclipse 8.6.1 制作绿色版
我们先在这个目录下新建一个文件: MyEclipse 10.6.bat , 文件内容如下: start eclipse\eclipse.exe -vm jre\bin\javaw.exe 接下来只需要 ...
- git+jenkins jar包代码的发布加新建项目
1.本地仓库 java开发 把代码上传上来 ,问一下他要上传到的主机ip , 分支 2.本地 , 设置-->仓库 更新数据,让他同步到南阳gitlab, 若没有这个项目,需要创建相同名字的项目 ...
- StringGrid换行功能
关闭stringgrid的defaultdrawing功能 StringGrid1.Cells[cCol,cRow] := '测试1'+#13#10+'测试2'; procedure TForm1.S ...
- HiBench成长笔记——(8) 分析源码workload_functions.sh
workload_functions.sh 是测试程序的入口,粘连了监控程序 monitor.py 和 主运行程序: #!/bin/bash # Licensed to the Apache Soft ...
- 编程练习:求某个数的n次方,返回其个位和十位
#!/usr/bin/env python def pow1(n,m): if m==0: return 1 if m==-1: return (1/n) if m & 1 != 0: ret ...
- 数据交互与ajax
在Long Long Ago,那个前端还是一个切图仔的年代,那时的页面根本没有js,前端的api非常的少,页面的所有数据都来自服务器渲染,任何的页面操作都会提交form表单请求刷新页面,直到那一天,浏 ...
- java核心-多线程(9)- ThreadLocal类
1.背景 ThreadLocal类我想一般的码农或初级程序员在平时开发中基本上接触不到,但是面试老师会问.往高级点走会遇到这个类.这个类不是为了解决资源的竞争问题,而是为每个线程提供同一个容器 ...
- 002、创建第一个Java程序HelloWord
代码如下: package TIANPAN; public class TestDemo { public static void main(String args[]) { System.out.p ...