rt,有人能帮我改对的话我用三个号给他关注,Code:
#include<iostream>
#include<algorithm>
#define int long long
#define rep for(int i=head[u];i!=-1;i=e[i].next)
#define clear(a) for(int i=0;i<N;++i)a[i]=0;
using namespace std;
const int N=1e6+5;
int mark;
int n,m,p,l,r,ans,cnt;
int head[N],dep[N],d[N],dist[N];
int jump[N][25];
struct edge{
int v,w,next;
}e[N];
inline void add(int u,int v,int w){
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
return ;
}
inline void dfsA(int u,int f){
if(d[u]>d[p])p=u;
rep{
int v=e[i].v,w=e[i].w;
if(v==f)continue;
d[v]=d[u]+w;
dfsA(v,u);
}
return ;
}
inline void dfsB(int u,int f){
rep{
int v=e[i].v;
if(v==f)continue;
dep[v]=dep[u]+1;
jump[v][0]=u;
for(int i=1;i<=20;++i){
jump[v][i]=jump[jump[v][i-1]][i-1];
}
dfsB(v,u);
}
return ;
}
inline void recPath(){
dfsA(1,0);
r=p;
d[r]=0;
dfsA(r,0);
l=p;
return ;
}
inline void recEdge(int u,int f){
rep{
int v=e[i].v;
if(v==f)continue;
dist[v]=dist[u]+e[i].w;
recEdge(v,u);
}
return ;
}
inline int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=20;i>=0;--i){
if(dep[jump[x][i]]>=dep[y]){
x=jump[x][i];
}
if(dep[x]==dep[y])break;
}
for(int i=20;i>=0;--i){
if(jump[x][i]!=jump[y][i]){
x=jump[x][i];
y=jump[y][i];
}else{
break;
}
}
return x==y?x:jump[x][0];
}
inline int dis(int x,int y){
return dist[x]+dist[y]-dist[LCA(x,y)]*2;
}
signed main(){
// freopen("in.txt","r",stdin);
for(int i=1;i<N;++i)head[i]=-1;
cin>>n>>m;
for(int i=1;i<=n-1;++i){
int u,v,w;
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
recPath();
dfsB(1,0);
// cout<<"done\n";
recEdge(1,0);
int b=l,c=r;
for(int a=1;a<=n;++a){
ans=max(ans,min(dis(a,b),dis(a,c)));
}
cout<<ans+dis(b,c)<<"\n";
return 0;
}
//Wu_Ruiheng 4980pts rk526
//Lywh_ddAC 3331pts rk1234