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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
| #include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <cctype>
#define pp pair<int,int>
using namespace std;
namespace fast_io {
//...
}using namespace fast_io;
const int MAXN = 310000;
struct Edge{
int from,to;
int len;
};
int n,k;
vector<Edge> edge[MAXN];
void init(){
read(n),read(k);
int a,b,c;
for(int i = 1;i<=n-1;i++){
read(a),read(b),read(c);
edge[a].push_back((Edge){a,b,c});
edge[b].push_back((Edge){b,a,c});
}
}
int dis[MAXN],f[MAXN];
void dfs(int nown,int fa){
f[nown] = fa;
for(int i = 0;i<edge[nown].size();i++){
int v = edge[nown][i].to,l = edge[nown][i].len;
if(v == f[nown]) continue;
dis[v] = dis[nown] + l;
dfs(v,nown);
}
}
int getmax(int nown){
int ans = dis[nown];
for(int i = 0;i<edge[nown].size();i++){
int v = edge[nown][i].to;
if(v == f[nown]) continue;
ans = max(ans,getmax(v));
}
return ans;
}
int num[MAXN],d[MAXN],maxn[MAXN],tot = 0;
void build(){
int u = 0,v = 0;
dis[1] = 0;
dfs(1,0);
for(int i = 1;i<=n;i++)
if(dis[u] < dis[i]) u = i;
dis[u] = 0;
dfs(u,0);
for(int i = 1;i<=n;i++)
if(dis[v] < dis[i]) v = i;
for(int i = v;i;i = f[i])
num[++tot] = i;
reverse(num+1,num+tot+1);
for(int i = 1;i<=tot;i++)
d[i] = dis[num[i]];
for(int i = 1;i<=tot;i++){
int nown = num[i];
for(int j = 0;j<edge[nown].size();j++){
int v = edge[nown][j].to;
if(v == num[i+1] || v == num[i-1]) continue;
maxn[i] = max(maxn[i],getmax(v));
}
if(maxn[i]) maxn[i] -= d[i];
}
}
void solve(){
deque<pp> q;
int l = 1,ans = 0x3f3f3f3f;
for(int i = 1;i<=tot;i++){
while(!q.empty() && q.back().second < maxn[i])
q.pop_back();
q.push_back(make_pair(i,maxn[i]));
while(d[i] - d[l] > k)
l++;
while(!q.empty() && q.front().first < l)
q.pop_front();
ans = min(ans,max(max(d[l],d[tot] - d[i]),q.front().second));
}
printf("%d\n",ans);
}
int main(){
init();
build();
solve();
flush();
return 0;
}
|