题目大意:给定一棵 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. SparkCore的性能优化

    1.广播变量 1.1. Spark提供的Broadcast Variable,是只读的,并且在每个节点上只会有一份副本,而不会为每个task都拷贝一份副本 1.2.它的最大作用,就是减少变量到各个节点 ...

  2. 微信小程序页面阻止默认滑动事件

    在页面上要加入一个悬浮的按钮,这个按钮需要可以拖动,在元素中使用catchtouchstart,catchtouchmove,catchtouchend来控制悬浮按钮的拖动,但是在ios系统中,微信小 ...

  3. linux如何设置磁盘配额?

    环境:CentOS7 需求:  zhang3 用户在  /dev/sdb1  上,只能使用100M的空间,建立 200个文件. 注意事项:XFS  不能对  /   开启配额             ...

  4. 如何将其它javaweb项目变成可以成功在自己eclipse环境中运行的javaweb项目?

    说明:此文档仅适用于以下两种情况     (1)myeclipse项目需要在eclipse环境中运行     (2)eclipse项目,但是无法在自己的电脑eclipse环境中运行     注意:以下 ...

  5. Durable NAND flash memory management

    词条积累 1.NAND flash memory http://www.searchstorage.com.cn/whatis/word_6052.htm http://baike.baidu.com ...

  6. 谷歌云SSH开启root密码登陆

    废话不多说,开始教程 1.先选择从浏览器打开ssh连接服务器连接登录成功后,输入以下命令 sudo -i #切换到root passwd #修改密码 然后会要求输入新密码,然后再重复一次密码,输入密码 ...

  7. 平面割线平分点(构造)--牛客第三场-- Magic Line

    题意: 给你n个点的坐标,让你给出两个点,这两个点的连线可以平分这些点. 思路: 先按y的大小排序,在按x的小排序,再搞一下就行了.如下图: #include <bits/stdc++.h> ...

  8. # 江西ccpc省赛-waves-(DP做法)

    江西ccpc省赛-waves-(DP做法) 题链:http://acm.hdu.edu.cn/showproblem.php?pid=6570 题意:给你长度为N,1≤N≤100000的一个数组,其中 ...

  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. file_put_contents实现内容追加

    file_put_contents("test.txt", "This is another something.", FILE_APPEND); FILE_A ...