E - Minimum Spanning Tree Gym - 102220E (转化+贡献)
In the mathematical discipline of graph theory, the line graph of a simple undirected weighted graph G is another simple undirected weighted graph L(G) that represents the adjacency between every two edges in G
.
Precisely speaking, for an undirected weighted graph G
without loops or multiple edges, its line graph L(G)
is a graph such that:
- Each vertex of L(G)
represents an edge of G
- .
- Two vertices of L(G)
are adjacent if and only if their corresponding edges share a common endpoint in G
- , and the weight of such edge between this two vertices is the sum of their corresponding edges' weight.
A minimum spanning tree(MST) or minimum weight spanning tree is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. That is, it is a spanning tree whose sum of edge weights is as small as possible.
Given a tree G
, please write a program to find the minimum spanning tree of L(G)
.
Input
The first line of the input contains an integer T(1≤T≤1000)
, denoting the number of test cases.
In each test case, there is one integer n(2≤n≤100000)
in the first line, denoting the number of vertices of G
.
For the next n−1
lines, each line contains three integers u,v,w(1≤u,v≤n,u≠v,1≤w≤109), denoting a bidirectional edge between vertex u and v with weight w
.
It is guaranteed that ∑n≤106
.
Output
For each test case, print a single line containing an integer, denoting the sum of all the edges' weight of MST(L(G))
.
Example
2
4
1 2 1
2 3 2
3 4 3
4
1 2 1
1 3 1
1 4 1
8
4
题解:题目给出一张图,让我们将每个边看成一个”点“,两“点”之间的权值为两边权之和。让我们找到这个“点”组成的图(题目命名为”线图“)的最小生成树的权值和。
我们可以从每条边权(即每个点的出边)的贡献入手,首先一个点的出边必须连通,否则构不成最小生成树。
那么对于特定的一个点,首先将其所有出边全部的权值加一遍,然后将其最小的一个边权乘以(这一点的度degree-2)即保证了最优解。(其实这样就是连degree-1条边使得保证最优解)
对于每一个点都这样,跑一遍即可。
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<stdio.h>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=;
struct node
{
int v,w;
bool operator < (const node &r)const{
return w<r.w;
}
};
vector<node>G[maxn];
int main()
{
ios::sync_with_stdio();
int T;
cin>>T;
while(T--){
int n;
cin>>n;
for(int i=;i<=n;i++)G[i].clear();
for(int i=;i<=n-;i++){
int u,v,w;
cin>>u>>v>>w;
G[u].push_back((node){v,w});
G[v].push_back((node){u,w});
}
ll ans=;
for(int i=;i<=n;i++){
sort(G[i].begin(),G[i].end());
int minn=0x3f3f3f3f;
int degree=G[i].size();
for(int j=;j<degree;j++){
ans+=G[i][j].w;
minn=min(minn,G[i][j].w);
}
ans+=(ll)minn*(degree-);//当degree为1时与上面的ans相互消去
}
cout<<ans<<endl;
}
return ;
}
E - Minimum Spanning Tree Gym - 102220E (转化+贡献)的更多相关文章
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- 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 ...
- 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 ...
- 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 ...
- MST(Kruskal’s Minimum Spanning Tree Algorithm)
You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- 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 ...
随机推荐
- POJ 1466:Girls and Boys 二分图的最大点独立集
Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 11097 Accepted: 4960 D ...
- POJ 2251:Dungeon Master
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20687 Accepted: 8004 D ...
- 备份 分区表 mbr
备份方法: 1.备份分区表信息 sudo fdisk -l >hda.txt #分区表信息重定向输出到文件中 2.备份MBR linux@linux-desktop:~/ex$ sudo ...
- 使用connected-react-router使router与store同步
connected-react-router是一个绑定react-router到redux的组件,来实现双向绑定router的数据到redux store中,这么做的好处就是让应用更Redux化,可以 ...
- contos7 共享文件夹开机自动挂载
网上很多文章都说改文件/etc/fstab 我试了很多次都不行 然后看到另一个方法 在/etc/rc.d/rc.local 增加挂在脚本这个时候要注意执行权限问题 我是这样做的 sudo mount ...
- .NET技术-2.0. 操作数据库-Dapper
.NET技术-2.0. 操作数据库-Dapper 项目参见: 1. 为什么选择Dapper 1) 性能优越: 其实在各大网站上,我们大概都会看到这样的一个对比效果图,在超过500次poco seria ...
- 直击JDD | 京东开启技术服务元年:携手合作伙伴,共创产业未来
11月19日,主题为"突破与裂变"的2019京东全球科技探索者大会(JDDiscovery)在京盛大开幕.京东集团副总裁黎科峰在JDD主论坛做了题为"技术驱动.开放赋能& ...
- 代码杂谈-python函数
发现函数可以设置属性变量, 如下 newfunc.func , newfunc.args def partial(func, *args, **keywords): """ ...
- 基于表单的web暴力破解
暴力破解 概述 连续性尝试+字典+自动化 如果一个网站没有对登录接口实施防暴力破解的措施,或者实施了不合理的措施,则该网站存在暴力破解漏洞. 是否要求用户设置了复杂的密码 是否每次认证都是用安全的验证 ...
- “杀死”纸质名片!HiHello能重构商业关系网吗?
在当下的互联网时代,要添加好友去扩大自己的社交圈似乎是再简单不过.随便点击一个微信名片.与其他网友互相关注微博等,好像就又搭建了一个社交节点.暂且不讨论这些好友关系的质量问题,单是这样的方式并不适合于 ...