# 积累程度 poj3585

N≤2∗105N≤2∗105

1
5
1 2 11
1 4 13
3 4 5
4 5 10


26

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;

using namespace std;
const int N=700100;
int n;
bool v[N];
inline void adds(int x,int y,int z)
{
tot++;
point[tot]=y;
edge[tot]=z;
}

void dfsd(int x)
{
v[x]=1;
d[x]=0;
{
int y=point[i];
if(v[y])
continue;

dfsd(y);
if(du[y]==1)
d[x]+=edge[i];
else
d[x]+=min(edge[i],d[y]);
}
}

void dfs(int x)
{
v[x]=1;
{
int y=point[i];
if(v[y])
continue;

if(du[x]==1)
dp[y]=edge[i]+d[y];
else
{
dp[y]=d[y]+min(dp[x]-min(d[y],edge[i]),edge[i]);
}
dfs(y);
}
}

int main()
{
int jb;
scanf("%d",&jb);
while(jb--)
{
tot=0;
memset(Next,0,sizeof(Next));

memset(du,0,sizeof(du));
cin>>n;
for(int i=1; i<n; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
du[x]++,du[y]++;
}
int root=1;
memset(v,0,sizeof(v));
dfsd(root);
dp[root]=d[root];
memset(v,0,sizeof(v));

dfs(root);

ll ans=-1;

for(int i=1; i<=n; i++)
{
ans=max(ans,dp[i]);
//cout<<dp[i]<<' '<<d[i]<<endl;

}
printf("%lld\n",ans);
}
}