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 <bits/stdc++.h>
using namespace std ;
const int N = 500010 , M = N < < 1 ;
# define int long long
int n , m , k ;
# define INF 1e18
int sz [ N ] ; // 以u为根节点是否有人
int g [ N ] ; // 从u出发把u子树上的人都送回家再回到u所需要的时间
vector < pair < int , int > > vec [ N ] ;
int max1 [ N ] ; // u的子树中最长链的长度
int max2 [ N ] ; // u的子树中次长链
int up [ N ] ; // 不在u的子树内, 距离u最远的那个人的家到u的距离
int id [ N ] ; // 最长链条是哪条链
int ans [ N ] ; // 从u出发把所有点都送回家再回到u的结果
// ans[i] -max(up[i],max1[i]);
void dfs1 ( int u , int fa ) {
for ( auto ts : vec [ u ] ) {
int v = ts . first ;
int w = ts . second ;
if ( v = = fa ) continue ;
dfs1 ( v , u ) ;
if ( sz [ v ] ) {
g [ u ] + = g [ v ] + 2 * w ;
int now = max1 [ v ] + w ;
if ( now > = max1 [ u ] ) {
max2 [ u ] = max1 [ u ] ;
max1 [ u ] = now ;
id [ u ] = v ;
} else if ( now > = max2 [ u ] ) {
max2 [ u ] = now ;
}
}
sz [ u ] + = sz [ v ] ;
}
}
void dfs2 ( int u , int fa ) {
for ( auto ts : vec [ u ] ) {
int v = ts . first ;
int w = ts . second ;
if ( v = = fa ) continue ;
if ( sz [ v ] = = k ) {
ans [ v ] = g [ v ] ;
up [ v ] = 0 ;
} else if ( sz [ v ] = = 0 ) {
ans [ v ] = ans [ u ] + 2 * w ;
up [ v ] = max ( up [ u ] , max1 [ u ] ) + w ;
} else if ( sz [ v ] & & sz [ v ] ! = k ) {
ans [ v ] = ans [ u ] ;
if ( id [ u ] = = v ) {
up [ v ] = max ( max2 [ u ] , up [ u ] ) + w ;
} else
up [ v ] = max ( up [ u ] , max1 [ u ] ) + w ;
}
dfs2 ( v , u ) ;
}
}
signed main ( ) {
cin > > n > > k ;
for ( int i = 1 ; i < n ; i + + ) {
int a , b , c ;
cin > > a > > b > > c ;
vec [ a ] . push_back ( { b , c } ) ;
vec [ b ] . push_back ( { a , c } ) ;
}
for ( int i = 1 ; i < = k ; i + + ) {
int x ;
cin > > x ;
sz [ x ] = 1 ;
}
dfs1 ( 1 , 0 ) ;
ans [ 1 ] = g [ 1 ] ;
dfs2 ( 1 , 0 ) ;
int minn = INF ;
for ( int u = 1 ; u < = n ; u + + ) cout < < ans [ u ] - max ( up [ u ] , max1 [ u ] ) < < endl ;
}