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

Input
  1. 2
  2. 4
  3. 1 2 1
  4. 2 3 2
  5. 3 4 3
  6. 4
  7. 1 2 1
  8. 1 3 1
  9. 1 4 1
Output
  1. 8
  2. 4

  3. 题解:题目给出一张图,让我们将每个边看成一个”点“,两“点”之间的权值为两边权之和。让我们找到这个“点”组成的图(题目命名为”线图“)的最小生成树的权值和。
    我们可以从每条边权(即每个点的出边)的贡献入手,首先一个点的出边必须连通,否则构不成最小生成树。
    那么对于特定的一个点,首先将其所有出边全部的权值加一遍,然后将其最小的一个边权乘以(这一点的度degree-2)即保证了最优解。(其实这样就是连degree-1条边使得保证最优解)
    对于每一个点都这样,跑一遍即可。
  1. #include<iostream>
  2. #include<cstring>
  3. #include<string>
  4. #include<queue>
  5. #include<stack>
  6. #include<algorithm>
  7. #include<stdio.h>
  8. #include<map>
  9. #include<set>
  10. using namespace std;
  11. typedef long long ll;
  12. const int maxn=;
  13. struct node
  14. {
  15. int v,w;
  16. bool operator < (const node &r)const{
  17. return w<r.w;
  18. }
  19. };
  20. vector<node>G[maxn];
  21. int main()
  22. {
  23. ios::sync_with_stdio();
  24. int T;
  25. cin>>T;
  26. while(T--){
  27. int n;
  28. cin>>n;
  29. for(int i=;i<=n;i++)G[i].clear();
  30. for(int i=;i<=n-;i++){
  31. int u,v,w;
  32. cin>>u>>v>>w;
  33. G[u].push_back((node){v,w});
  34. G[v].push_back((node){u,w});
  35. }
  36. ll ans=;
  37. for(int i=;i<=n;i++){
  38. sort(G[i].begin(),G[i].end());
  39. int minn=0x3f3f3f3f;
  40. int degree=G[i].size();
  41. for(int j=;j<degree;j++){
  42. ans+=G[i][j].w;
  43. minn=min(minn,G[i][j].w);
  44. }
  45. ans+=(ll)minn*(degree-);//当degree为1时与上面的ans相互消去
  46. }
  47. cout<<ans<<endl;
  48. }
  49. return ;
  50. }

E - Minimum Spanning Tree Gym - 102220E (转化+贡献)的更多相关文章

  1. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  2. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

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

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

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

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

  7. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

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

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

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

随机推荐

  1. CountUp.js 数字跳转效果小插件

    CountUp.js  实现数字跳转效果的小插件 //调用方法 const easingFn = function (t, b, c, d) { var ts = (t /= d) * t; var ...

  2. go 的参数传递

    再go语言中没有引用传递,所有都是按照值拷贝的方式传递的. 数组:实际就是堆栈上的一段连续内存,和c类似.(可以更加反编译代码推断 go tool compile -S main.go > ma ...

  3. ci框架与smarty的整合

    ci框架与smarty的整合 来源:未知    时间:2014-10-20 11:38   阅读数:108   作者:xbdadmin [导读] Ci 和 smarty 的完美结合 Ci 结合 sma ...

  4. UVA 127 链表和栈的使用

    刘汝佳的题目感觉都是比较难以处理的,就像这道题目,一看数据简直觉得头大...加上这个英文我也看的想死 最后看别人博客的题意讲解才知道原来是要移牌. 然后如果熟练的使用stack和手写链表的话,这个题目 ...

  5. LeetCode——139. 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...

  6. junit中@Before和@BeforeClass区别

    @before 在每个测试方法之前都执行一次, 方法需要声明为public @beforeclass 只在类中执行一次, 必须声明为public static

  7. 模拟jenkins通过shell给ansible传入变量

    jenkins.sh #!/bin/bash name1='robin h h li' age1='11' declare -A dic dic=( [name1]="${name1}&qu ...

  8. TextView和Button的学习

    常用属性,界面跳转,按钮学习,按压颜色的变换,图片的插入学习等 工程目录: MainActivity.java: package com.example.revrse; import androidx ...

  9. Django模型基础——(二)

    上篇博客主要讲了django中对数据库的增删改查,下面深入再讲解下对数据库的操作. 常用的查询方法 下面以表名为User为例 User.object.first() :返回表中第一条数据 User.o ...

  10. Ribbon使用及其客户端负载均衡实现原理分析

    1.ribbon负载均衡测试 (1)consumer工程添加依赖 <dependency> <groupId>org.springframework.cloud</gro ...