找出一条直径,以直径上的每个点为开头或结尾,分别求它的ECC即可

可以证明的是,如果图中存在多条路径,则在任何一条直径上都存在一条core(反证法,用到直径的距离最大性)。因此只需讨论一条直径上的core的情况即可。

 program core;
type
  node=record
    x,y:integer;
  end;
var
  ans:longint;
  n,s:integer;
  f:array[1..300,1..300] of longint;
  w:array[1..300,0..300] of integer;
  d:array[1..300] of longint;
  a:array[0..300] of longint;
  p:node;
procedure init;
var
  i,j,p,q,v:integer;
begin
  fillchar(f,sizeof(f),$5F);
  readln(n,s);
  for i:=1 to n do
  begin
    f[i,i]:=0;
    for j:=i+1 to n do
    begin
      f[i,j]:=1000000;
      f[j,i]:=1000000;
    end;
  end;
  for i:=1 to n-1 do
  begin
    readln(p,q,v);
    f[p,q]:=v;
    f[q,p]:=v;
    inc(w[p,0]);
    w[p,w[p,0]]:=q;
    inc(w[q,0]);
    w[q,w[q,0]]:=p;
  end;
end;
procedure floyd;
var
  i,j,k:integer;
begin
  for k:=1 to n do
    for i:=1 to n do
      for j:=1 to n do
        if f[i,k]+f[k,j]<f[i,j] then f[i,j]:=f[i,k]+f[k,j];
end;
procedure findd;
var
  i,j:integer;
  max:longint;
begin
  fillchar(p,sizeof(p),0);
  max:=-maxint;
  for i:=1 to n-1 do
    for j:=i+1 to n do
      if f[i,j]>max then
      begin
        p.x:=i;
        p.y:=j;
        max:=f[i,j];
      end
      else if f[i,j]=max then
      begin
        p.x:=i;
        p.y:=j;
      end;
end;
procedure findecc;
var
  flag:array[1..300] of boolean;
  i,j,k,l,r,pre:longint;
  max:longint;
begin
  ans:=maxint;
  i:=1;
  fillchar(a,sizeof(a),0);
  fillchar(flag,sizeof(flag),false);
  r:=p.x;
  a[0]:=1;
  a[1]:=r;
  flag[r]:=true;
  while r<>p.y do
  begin
    l:=r;
    for j:=1 to w[l,0] do
      if (f[p.x,w[l,j]]+f[w[l,j],p.y]=f[p.x,p.y]) and not flag[w[l,j]] then
      begin
        inc(a[0]);
        a[a[0]]:=w[l,j];
        r:=w[l,j];
        flag[w[l,j]]:=true;
        break;
      end;
  end;
  fillchar(d,sizeof(d),$5F);
  for j:=1 to a[0] do
  begin
    l:=a[j];
    for k:=1 to n do
      d[k]:=f[l,k];
    for k:=j+1 to a[0] do
    begin
      r:=a[k];
      if f[l,r]<=s then
      begin
        for pre:=1 to n do
          if f[r,pre]<d[pre] then d[pre]:=f[r,pre];
      end;
    end;
    max:=-maxint;
    for k:=1 to n do
      if d[k]>max then
      begin
        max:=d[k];
        if max>=ans then break;
      end;
    if max<ans then ans:=max;
  end;
  writeln(ans);
end;
begin
  assign(input,'core.in');
  reset(input);
  assign(output,'core.out');
  rewrite(output);
  init;
  floyd;
  findd;
  findecc;
  close(input);
  close(output);
end.