题目大意:给定一棵 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. NJCTF (easycrack)

    安装app查看.一个输入框,输入随便输入显示Try again. 放入JEB反编译. 关于输入框监听是第一次见,具体可以看看这个博客https://www.jianshu.com/p/f976c677 ...

  2. PJzhang:shell基础入门的2个疗程-two

    猫宁!!! 第6节:重定向 标准输入,标准输出,错误输出 输入重定向符号'<' 输出重定向符号'>'(清空之后再输入),'>>'(当前内容不变,在最后一行追加),'2>' ...

  3. C语言之联合体

    联合union是一个能在同一个存储空间存储不同类型数据的类型 联合体所占的内存长度等于其最长成员的长度,也有叫做共用体 联合体虽然可以有多个成员,但同一时间只能存放其中一种 对于联合体来讲最基本的原则 ...

  4. pikachu-SQL注入

    参考网址: http://www.mamicode.com/info-detail-2795438.html

  5. PTA(Advanced Level)1011.World Cup Betting

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...

  6. 搞懂MySQL GTID原理

    从MySQL 5.6.5 开始新增了一种基于 GTID 的复制方式.通过 GTID 保证了每个在主库上提交的事务在集群中有一个唯一的ID.这种方式强化了数据库的主备一致性,故障恢复以及容错能力. GT ...

  7. 厉害了,ES 如何做到几十亿数据检索 3 秒返回!

    一.前言 数据平台已迭代三个版本,从头开始遇到很多常见的难题,终于有片段时间整理一些已完善的文档,在此分享以供所需朋友的 实现参考,少走些弯路,在此篇幅中偏重于ES的优化,关于HBase,Hadoop ...

  8. 区间前k小的和(权值线段树+离散化)--2019牛客多校第7场C--砍树

    题目链接:https://ac.nowcoder.com/acm/contest/887/C?&headNav=acm 题意: 给你 n 种树,有 高度,花费和数量 ,现在问你最少需要花多少钱 ...

  9. linux centos7.3安装lnmp,nginx-1.11.12 ,php7.0.2 ,

    #更新源 yum -y update #添加用户和组 adduser www groupadd www usermod -G www www #初始化目录 mkdir -p /data/app/php ...

  10. ZuulServlet源码分析及ZuulFilter加载

    参考https://yq.aliyun.com/wenji/2...https://blog.csdn.net/lds2227... 1.声明ZuulServlet @Configuration @E ...