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 ...
随机推荐
- Day3-M-Cable master POJ1064
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...
- Day3-D-Protecting the Flowers POJ3262
Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When ...
- SSH框架结构分析
分类: 工作问题2012-03-29 18:10 1511人阅读 评论(0) 收藏 举报 框架sshhibernatespringstrutsdao 最近在弄j2ee,发现还是学到了很多东西,心情ha ...
- Java 推荐读物与源代码阅读
Java 推荐读物与源代码阅读 江苏无锡 缪小东 1. Java语言基础 谈到Java ...
- JuJu团队1月9号工作汇报
JuJu团队1月9号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 飞飞 将示例程序打包成exe 将crossentrophy和softmax连接起来 无 婷婷 -- 完善ma ...
- greenplum 存储过程 索引信息
涉及的索引表 参考:http://blog.nbhao.org/1539.html pg_index pg_indexes pg_stat_all_indexes # 记录当前数据库中所有的索引的使用 ...
- swoole之任务和定时器
一.代码 <?php use Swoole\Server; /** * 面向对象的形式 + task + timer */ class WebSocket { public $server; p ...
- java 饮料换购
饮料换购 乐羊羊饮料厂正在举办一次促销优惠活动.乐羊羊C型饮料,凭3个瓶盖可以再换一瓶C型饮料,并且可以一直循环下去,但不允许赊账. 请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,那么,对于他初始 ...
- P1058 选择题
P1058 选择题 转跳点:
- HTML学习第七天(二)
HTML学习第七天(二) 新增的非主体结构 header元素 footer元素 hgroup元素 address元素 header元素:通常用于存放整个页面或页面内的一个区域块的标题,但也可以存放类似 ...