数据太水, 神奇错误代码竟得91分
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=10000005;
int n, s;
int lft, rgh;
int ans=INT_MAX, maxn, sum[N];
int pre[N];
int path[N], num;
int lng[N];
int dep[N];
bool vis[N];
struct edge{
int to, w, nex;
} edge[N];
int head[N], tot=1;
void add(int x, int y, int z) {
edge[++tot]={y, z, head[x]};
head[x]=tot;
}
queue<int> q;
int dis[N];
int bfs(int s) {
int e=0, maxn=0;
memset(dis, -1, sizeof dis);
q.push(s);
dis[s]=0;
while (q.size()) {
int x=q.front();
q.pop();
for (int i=head[x]; i; i=edge[i].nex) {
int y=edge[i].to, z=edge[i].w;
if (dis[y]!=-1) continue;
dis[y]=dis[x]+z, pre[y]=i;
if (dis[y]>maxn) maxn=dis[y], e=y;
q.push(y);
}
}
return e;
}
void get() { //yes
while (lft!=rgh) {
path[++num]=rgh;
lng[num+1]=edge[pre[rgh]].w;
vis[rgh]=1;
rgh=edge[pre[rgh]^1].to;
}
path[++num] = rgh;
vis[rgh] = 1;
}
void dfs(int x) { //yes
vis[x]=1;
for (int i=head[x]; i; i=edge[i].nex) {
int y=edge[i].to, z=edge[i].w;
if (vis[y]) continue;
dfs(y);
dep[x]=max(dep[x], dep[y]+z);
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> s;
int x, y, z;
for (int i=1; i<n; ++i) {
cin >> x >> y >> z;
add(x, y, z), add(y, x, z);
}
lft=bfs(1); rgh=bfs(lft);
get();
for (int i=1; i<=num; ++i) {
dfs(path[i]);//原来是dfs(i);
maxn=max(maxn, dep[path[i]]);
sum[i]=sum[i-1]+lng[i];
}
for (int i=1, j=1; i<=num; ++i) {
while (j<num && sum[j+1]-sum[i]<=s) ++j;
ans=min(ans, max(max(sum[i], sum[num]-sum[j]), maxn));
}
cout << ans << '\n';
return 0;
}