题目大意:给定一棵 N 个点的树,边有边权,定义“线树”为一个图,其中图的顶点是原树中的边,原树中两条有公共端点的边对应在线图中存在一条边,边权为树中两条边的边权和,求线图的最小生成树的代价是多少。

题解:

对于树中的一个顶点来说,假设有 M 条边以该顶点为一个端点,那么这 M 条边对应到线图中的顶点必须要求能够构成一个联通块。另外,可以发现这个问题的解决和其他顶点无关,即:对于树上每个顶点来说,构成了一个子问题。因此,考虑一个贪心策略,即:每次用边权最小的那条边和其他所有边相连,这样的代价是最小的。可以发现每条边仅被考虑两次(两个端点各考虑一次),因此总复杂度为 \(O(M)\)。

代码如下

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
typedef long long LL; int n;LL ans;
struct node{
int nxt,to;LL w;
}e[maxn<<1];
int tot=1,head[maxn];
inline void add_edge(int from,int to,int w){
e[++tot]=node{head[from],to,w},head[from]=tot;
} void dfs(int u,int fa,LL fe){
LL mi=0x3f3f3f3f,cnt=0,ret=0;
if(fe!=-1)mi=min(mi,fe),ret=fe,cnt=1;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;LL w=e[i].w;
if(v==fa)continue;
ret+=w,mi=min(mi,w),++cnt;
dfs(v,u,w);
}
LL res=ret+(cnt-2)*mi;
ans+=res;
}
void read_and_parse(){
scanf("%d",&n);
for(int i=1,x,y,z;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
add_edge(x,y,z),add_edge(y,x,z);
}
}
void solve(){
dfs(1,0,-1);
printf("%lld\n",ans);
}
void init(){
tot=1,ans=0;
for(int i=1;i<=n;i++)head[i]=0;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
init();
read_and_parse();
solve();
}
return 0;
}

【The 13th Chinese Northeast Collegiate Programming Contest E题】的更多相关文章

  1. 【The 13th Chinese Northeast Collegiate Programming Contest H 题】

    题目大意:NOIP2018d1t1 支持 M 次区间查询答案和区间修改操作. 题解: 首先考虑不带区间修改的情况.从左到右进行考虑,发现对于第 i 个数来说,对答案的贡献仅仅取决于第 i-1 个数的大 ...

  2. The 13th Chinese Northeast Collegiate Programming Contest

    题解: solution Code: A. Apple Business #include<cstdio> #include<algorithm> #include<ve ...

  3. The 13th Chinese Northeast Collegiate Programming Contest(B C E F H J)

    B. Balanced Diet 思路:把每一块选C个产生的价值记录下来,然后从小到大枚举C. #include<bits/stdc++.h> using namespace std; ; ...

  4. ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA

    ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the ...

  5. The 13th Zhejiang Provincial Collegiate Programming Contest - D

    The Lucky Week Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the headmaster of the Marja ...

  6. The 13th Zhejiang Provincial Collegiate Programming Contest - I

    People Counting Time Limit: 2 Seconds      Memory Limit: 65536 KB In a BG (dinner gathering) for ZJU ...

  7. The 13th Zhejiang Provincial Collegiate Programming Contest - C

    Defuse the Bomb Time Limit: 2 Seconds      Memory Limit: 65536 KB The bomb is about to explode! Plea ...

  8. 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest ---E题Entertainment Box(有点变化的贪心)

    提交链接 http://codeforces.com/gym/100781/submit Description: Ada, Bertrand and Charles often argue over ...

  9. The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)

    当时比赛时超时了,那时没学过树状数组,也不知道啥叫离散化(貌似好像现在也不懂).百度百科--离散化,把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 这道题是dp题,离散化和树状数 ...

随机推荐

  1. ApiDoc 后端接口注释文档的使用

    前端和后端注释文档生成 前端和后端的 函数及api 说明文档生成总结,持续更新中 by Qzx 参考网址 jsDoc - 中文说明 jsDoc 使用教程 后台接口文档生成器 - APIDOC官网 ap ...

  2. 注入之Mysql-Getshell思路

  3. 菜鸟系列Fabric——Fabric 1.2 多机部署(3)

    多机部署fabric kafka共识 1. 角色分配 主机1 主机 2 Org1 peer0 1 Org2 peer 0 1 Orderer 0 1 Orderer 2 kafka 0 1 kafka ...

  4. NXP-PN511-antenna-design-quide

    NXP-PN511-antenna-design-quide  文库有下载 C1 C2

  5. Elasticsearch-如何控制存储和索引文档(_source、_all、返回源文档的某些字段)

    Elasticsearch-如何控制存储和索引文档(_source._all) _source:可以在索引中存储文档._all:可以在单个字段上索引所有内容. 1. 存储原有内容的_source _s ...

  6. pyhton sort()无返回值

    li3=['] li4=li3.sort()#sort是没有返回值得,打印的是none,对列表进行升序排列 print(li4) print(li3) 输出结果是None['123', '123', ...

  7. ASP.NET Core WebApi使用Swagger生成API说明文档【xml注释版】

    ⒈新建ASP.NET Core WebAPi项目 ⒉添加 NuGet 包 Install-Package Swashbuckle.AspNetCore ⒊Startup中配置 using System ...

  8. 友善的树形DP

    一棵树,如果有序点对(x,y)之间路径的长度取模于3==0,那么ans0便加上这个长度: 如果取模于3==1,那么ans1便加上这个长度: 如果取模于3==2,那么ans2便加上这个长度: 让你求an ...

  9. 安装Composer与PsySH

    Windows安装Composer 需要开启 openssl 配置:打开 php 目录下的 php.ini,将 extension=php_openssl.dll 前面的分号去掉就可以了. https ...

  10. 基于VS搭建OpenCV环境

    OpenCV OpenCV的全称是Open Source Computer Vision Library,是一个跨平台的计算机视觉库.OpenCV是由英特尔公司发起并参与开发,以BSD许可证授权发行, ...