You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <iostream>
# include <cstdio>
# include <vector>
# include <cstring>
using namespace std ;
const int N = 2e5 + 10 , M = N < < 1 ;
int n , du [ N ] ;
// 链式前向星
int e [ M ] , h [ N ] , idx , w [ M ] , ne [ M ] ;
void add ( int a , int b , int c = 0 ) {
e [ idx ] = b , ne [ idx ] = h [ a ] , w [ idx ] = c , h [ a ] = idx + + ;
}
int d [ N ] , f [ N ] ;
void dfs1 ( int u , int fa ) {
for ( int i = h [ u ] ; ~ i ; i = ne [ i ] ) {
int v = e [ i ] ;
if ( v = = fa ) continue ;
dfs1 ( v , u ) ;
if ( du [ v ] = = 1 )
d [ u ] + = w [ i ] ;
else
d [ u ] + = min ( d [ v ] , w [ i ] ) ;
}
return ;
}
void dfs2 ( int u , int fa ) {
for ( int i = h [ u ] ; ~ i ; i = ne [ i ] ) {
int v = e [ i ] ;
if ( v = = fa ) continue ;
if ( du [ u ] = = 1 )
f [ v ] = d [ v ] + w [ i ] ;
else
f [ v ] = d [ v ] + min ( f [ u ] - min ( d [ v ] , w [ i ] ) , w [ i ] ) ;
dfs2 ( v , u ) ;
}
return ;
}
int main ( ) {
// 加快读入
ios : : sync_with_stdio ( false ) , cin . tie ( 0 ) ;
int T ;
cin > > T ;
while ( T - - ) {
// 初始化链式前向星
memset ( h , - 1 , sizeof h ) ;
idx = 0 ;
memset ( du , 0 , sizeof du ) ;
memset ( d , 0 , sizeof d ) ;
memset ( f , 0 , sizeof f ) ;
cin > > n ;
int a , b , c ;
for ( int i = 1 ; i < n ; i + + ) { // 树, n-1条无向边
cin > > a > > b > > c ;
add ( a , b , c ) , add ( b , a , c ) ;
du [ a ] + + , du [ b ] + + ; // 记录入度,无向图就不谈入度和出度了
}
dfs1 ( 1 , 0 ) ;
f [ 1 ] = d [ 1 ] ;
dfs2 ( 1 , 0 ) ;
int ans = 0 ;
for ( int i = 1 ; i < = n ; i + + ) ans = max ( ans , f [ i ] ) ;
cout < < ans < < endl ;
}
return 0 ;
}