题目链接: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 最小生成树变种的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)

    题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Summary Ranges leetcode

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  2. SQL server 数据库 ——聚合函数(一列 多行,值类型)

    聚合函数 5种函数: 1.max最大值   select max(price) from car where code='c024' 2.min最小值   select * from car wher ...

  3. 从客户端中检测到有潜在危险的 Request.Form或Requst.String的值。

    在ASP中客户端请求服务时会出现"从客户端中检测到有潜在危险的 Request.Form或Requst.QueryString的值.",原因是在web.config配置文件中存在这 ...

  4. Android Socket 遇到的Soure Not Find 错误

    参考: http://blog.csdn.net/brokge/article/details/8543145 http://blog.csdn.net/mad1989/article/details ...

  5. Octave Tutorial(《Machine Learning》)之第五课《控制语句和方程及向量化》

    第五课 控制语句和方程 For,while,if statements and functions (1)For loop v=zeros(10,1) %initial vectors for i=1 ...

  6. 浅析Content Negotation在Nancy的实现和使用

    背景介绍 什么是Content Negotation呢?翻译成中文的话就是"内容协商".当然,如果不清楚HTTP规范(RFC 2616)的话,可以对这个翻译也是一头雾水. 先来看看 ...

  7. (21)IO流之对象的序列化和反序列化流ObjectOutputStream和ObjectInputStream

    当创建对象时,程序运行时它就会存在,但是程序停止时,对象也就消失了.但是如果希望对象在程序不运行的情况下仍能存在并保存其信息,将会非常有用,对象将被重建并且拥有与程序上次运行时拥有的信息相同.可以使用 ...

  8. ES6是什么

    编程语言JavaScript是ECMAScript的实现和扩展,由ECMA(一个类似W3C的标准组织)参与进行标准化.编程语言JavaScript是ECMAScript的实现和扩展,由ECMA(一个类 ...

  9. java根据HashMap中的值将其元素排序

    思路:HashMap或Map本身没有排序功能,若要进行较轻松的排序,可利用ArrayList中的sort方法 例子: import java.util.ArrayList; import java.u ...

  10. letter-spacing+first-letter实现按钮文字隐藏

    本文地址:http://www.zhangxinxu.com/wordpress/?p=3557 一.文不在长,有货则灵 图片式按钮的文字隐藏看来是大家都比较关注的一个问题(分享讨论.微博转发等可见一 ...