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 ...
随机推荐
- Jquery属性操作、添加类
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Bug的等级及定位
缺陷等级一般划分为四个等级:致命.严重.一般.低 一.致命(一级bug) 通常表现为:系统无法运行,崩溃或严重资源不足,应用模块无法启动或者异常退出,主要功能模块无法使用. 比如: 1.系统崩溃(蓝屏 ...
- k8s 各种网络方案【转】
网络模型有了,如何实现呢? 为了保证网络方案的标准化.扩展性和灵活性,Kubernetes 采用了 Container Networking Interface(CNI)规范. CNI 是由 Core ...
- SpringBoot#ConfigurationProperties注解相关的一些知识
用途:ConfigurationProperties注解,用于在spring环境定义bean的时候.通过这个注解,把配置文件中的相关属性注入到实例化的bean中. 原理:spring中bean的生命周 ...
- WAFの基本防护透明流模式v1.0
一.WAFの透明流模式 1)首先先配置WAF的网络,配置一个网桥接口,设置IP便于带内管理. 2)当然,如果需要不同网段之间都能够管 ...
- Mongoose使用
文章来自 Mongoose基础入门 Mongoose的API Mongoose模式扩展 指南之查询 指南之验证 mongoose方法很多,很乱,版本不一样,有些方法可能都过时了,所以整理了很久 连接数 ...
- 002.让CI4框架CodeIgniter显示错误信息
01. 在public目录的index.php中,添加以下内容: //定义环境为开发模式,可以输出各种错误信息 define('ENVIRONMENT', 'development'); 02.我们在 ...
- Java 定时循环运行程序
Timer 和 ScheduledExecutorSeruvce 都能执行定时的循环任务,有函数 scheduleAtFixedRate.但是,如果任务运行时间较长,超过了一个周期时长,下一个任务就会 ...
- System.Reflection.ReflectionTypeLoadException
引用了一个第三方dll, 此dll经过混淆,但是未签名. 然后,主程序无法反射了, 取消主程序的签名后正常反射.
- php中常用的加密函数
1.MD5加密: string md5 ( string $str [, bool $raw_output = false ] ) (1)md5()默认情况下以 32 字符十六进制数字形式返回散列值, ...