1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
| #include <cstdio>
#include <vector>
#include <cctype>
#include <algorithm>
#define int long long
using namespace std;
namespace fast_io {
...//省略快读模版
}using namespace fast_io;
namespace normal_io{
inline char read(){
return getchar();
}
inline void read(int &x){
scanf("%lld",&x);
}
inline void print(int x){
printf("%lld",x);
}
inline void print(char x){
putchar(x);
}
inline void flush(){
return;
}
}using namespace normal_io;
const int MAXN = 510000;
int n,s;
struct Edge{
int t,l;
Edge(int b = 0,int c = 0):t(b),l(c){};
};
vector<Edge> edge[MAXN];
int ans[MAXN],times[MAXN];
bool vis[MAXN];
void addedge(int a,int b,int c){
edge[a].push_back(Edge(b,c));
edge[b].push_back(Edge(a,c));
}
void init(){
read(n),read(s);
int a,b,c;
for(int i = 1;i<=n-1;i++){
read(a),read(b),read(c);
addedge(a,b,c);
}
}
void dp(int nown){
vis[nown] = 1;
int tmpsum = 0,mintime = 0,k = 0;
for(int i = 0;i<edge[nown].size();i++){
int to = edge[nown][i].t,len = edge[nown][i].l;
if(vis[to] == 1)
continue;
dp(to);
ans[nown] += ans[to];
mintime = max(mintime,times[to]+len);
tmpsum += (times[to]+len);
k++;
}
ans[nown] += k * mintime - tmpsum,times[nown] = mintime;
}
void solve(){
dp(s);
print(ans[s]);
}
main(){
init();
solve();
flush();
return 0;
}
|