ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Minimum Spanning Tree Gym - 102220E

2020-12-31 12:32:45  阅读:200  来源: 互联网

标签:102220E weight graph tree edges GG Minimum Spanning line


 

 

In the mathematical discipline of graph theory, the line graph of a simple undirected weighted graph GG is another simple undirected weighted graph L(G)L(G) that represents the adjacency between every two edges in GG.

Precisely speaking, for an undirected weighted graph GG without loops or multiple edges, its line graph L(G)L(G) is a graph such that:

  • Each vertex of L(G)L(G) represents an edge of GG.
  • Two vertices of L(G)L(G) are adjacent if and only if their corresponding edges share a common endpoint in GG, 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 GG, please write a program to find the minimum spanning tree of L(G)L(G).

Input

The first line of the input contains an integer T(1≤T≤1000)T(1≤T≤1000), denoting the number of test cases.

In each test case, there is one integer n(2≤n≤100000)n(2≤n≤100000) in the first line, denoting the number of vertices of GG.

For the next n−1n−1 lines, each line contains three integers u,v,w(1≤u,v≤n,u≠v,1≤w≤109)u,v,w(1≤u,v≤n,u≠v,1≤w≤109), denoting a bidirectional edge between vertex uu and vv with weight ww.

It is guaranteed that ∑n≤106∑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))MST(L(G)).

Example

Input
2
4
1 2 1
2 3 2
3 4 3
4
1 2 1
1 3 1
1 4 1
Output
8
4

一棵 n 个点的树 G,G 的线图 L(G) 中连接 (u, v) 的边权为它们对应树上两条边的边权之和。 求 L(G) 的最小生成树的边权和。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int Case, n, i, j, x, y, z; 
vector<int>tree[N]; 
ll ans;
int main() {
    scanf("%d", &Case);
    while (Case--) {
        scanf("%d", &n);
        for (i = 1; i <= n; i++)tree[i].clear();
        for (i = 1; i < n; i++) {
            scanf("%d%d%d", &x, &y, &z);
            tree[x].push_back(z), tree[y].push_back(z);
        }
        ans = 0;
        for (i = 1; i <= n; i++) {
            if (tree[i].size() > 1) {
                sort(tree[i].begin(), tree[i].end());
                for (j = 1; j < tree[i].size(); j++)
                    ans += tree[i][0] + tree[i][j];
            }
        }
        printf("%lld\n", ans);
    }
}

 

标签:102220E,weight,graph,tree,edges,GG,Minimum,Spanning,line
来源: https://www.cnblogs.com/xxxsans/p/14215871.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有