#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 6100;
int n;
struct rec {int x, y, z;} e[6100];
int fa[6100], s[6100];
int ans;
bool operator <(rec a, rec b)
{
return a.z < b.z;
}
int get(int x)
{
if (x == fa[x]) return x;
return fa[x] = get(fa[x]);
}
signed main()
{
int tt;
cin >> tt;
while (tt--)
{
cin >> n;
ans = 0;
for (int i = 1; i <= n - 1; i ++)
cin >> e[i].x >> e[i].y >> e[i].z;
sort(e + 1, e + n);
for (int i = 1; i <= n; i ++)
fa[i] = i, s[i] = 1;
for (int i = 1; i < n; i ++)
{
int x = get(e[i].x);
int y = get(e[i].y);
if (x == y) continue;
ans += (s[x] * s[y] - 1) * (e[i].z + 1);
fa[x] = y;
s[y] += s[x];
}
cout << ans << endl;
}
return 0;
}
acwing 346
最新推荐文章于 2025-04-25 23:48:06 发布