最小生成树,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的更多相关文章

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

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

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

  3. Educational Codeforces Round 3 E (609E) Minimum spanning tree for each edge

    题意:一个无向图联通中,求包含每条边的最小生成树的值(无自环,无重边) 分析:求出这个图的最小生成树,用最小生成树上的边建图 对于每条边,不外乎两种情况 1:该边就是最小生成树上的边,那么答案显然 2 ...

  4. codeforces 609E. Minimum spanning tree for each edge 树链剖分

    题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...

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

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

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

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

随机推荐

  1. 修改Git的name和email

    对于git的user.name 与user.email来说,有三个地方可以设置 etc/gitconfig (几乎不常用) git config --system ~/.gitconfig(对于单个用 ...

  2. 前端学习笔记系列一:5 在项目中引入阿里图标icon

    进入到阿里的图标库网站,里面有上百万种icon,https://www.iconfont.cn,需要注册一个帐号,然后进入到这个页面,在这里点击右下角的带加号的图标,创建一个新的项目,名称与你要使用图 ...

  3. ubuntu 系统分配固定 ip--

    由于Ubuntu重启之后,ip很容易改变,可以用以下方式固定ip地址 1.设置ip地址 vi /etc/network/interface # The loopback network interfa ...

  4. volume 方式使用 Secret【转】

    Pod 可以通过 Volume 或者环境变量的方式使用 Secret,今天先学习 Volume 方式. Pod 的配置文件如下所示: ① 定义 volume foo,来源为 secret mysecr ...

  5. Codeforces 590 A:Median Smoothing

    A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. dMd----攻防世界

    首先在Linux上查看题目,没有什么发现elf文件,之后使用ida打开看看,找到main函数,f5查看, 上图一些字符是char过的,便于查看,发现是一个if else语句,先经过了MD5加密然后判断 ...

  7. BlackArch-Tools

    BlackArch-Tools 简介 安装在ArchLinux之上添加存储库从blackarch存储库安装工具替代安装方法BlackArch Linux Complete Tools List 简介 ...

  8. CVE-2019-0708 漏洞分析及相关测试

    在CVE-2019-0708公布后几天就已经尝试过复现该漏洞,但借助当时exp并没能成功复现反弹shell的过程遂放弃,故借助这次漏洞复现报告再来尝试复现该漏洞,因为还在大三学习中,有很多知识还没有掌 ...

  9. other#一些问题的列表

    centos7及以后修改hostname, hostnamectl set-hostname centos7 centos7之前修改hostname, vi /etc/sysconfig/networ ...

  10. [Android]如何导入已有的外部数据库

    转自:http://www.cnblogs.com/xiaowenji/archive/2011/01/03/1925014.html 我们平时见到的android数据库操作一般都是在程序开始时创建一 ...