#include<bits/stdc++.h>#define int long longusingnamespacestd;constintmaxn=1e6;intn,ans=0,h[maxn],dp[maxn];vector<int>G[maxn];voiddfs(intu,intpar){intmx=0,sec=0;for(autov:G[u]){if(v==par)continue;dfs(v,u);if(dp[v]>mx){sec=mx;mx=dp[v];}elseif(dp[v]>sec){sec=dp[v];}}dp[u]=mx+1;ans=max(ans,mx+sec);}signedmain(){cin>>n;intu,v;for(inti=0;i<n-1;i++){cin>>u>>v;G[u].push_back(v);G[v].push_back(u);}dfs(1,1);cout<<ans<<"\n";}
#include<bits/stdc++.h>#define int long long#define s second#define f first#define pii pair<int,int>usingnamespacestd;constintINF=0x3f3f3f3f,MAXN=1e5+5,M=1e9+7;intt,n,m,dis[MAXN],mx[MAXN];vector<vector<int>>G(MAXN);intdfs(intu,intp,intd){intans=u;dis[u]=max(d,dis[u]);for(autov:G[u]){if(v!=p){inttmp=dfs(v,u,d+1);if(dis[tmp]>dis[ans]){ans=tmp;}}}returnans;}signedmain(){ios_base::sync_with_stdio(0);cin.tie(0);t=1;while(t--){cin>>n;for(inti=0,u,v;i<n-1;i++){cin>>u>>v;G[u].push_back(v);G[v].push_back(u);}ints=dfs(1,0,0);intt=dfs(s,0,0);dfs(t,0,0);sort(dis+1,dis+1+n);intx=0;for(inti=1;i<=n;i++){while(x<=n&&dis[x]<i){x++;}if(x>n)cout<<n<<" ";elsecout<<x<<" ";}}}
#include<bits/stdc++.h>#define int long longusingnamespacestd;constintINF=2e18;constintMAXN=1e5+5;constintM=1e9+7;structNode{intmx,sec;};intn,m;vector<pair<int,int>>G[MAXN];vector<Node>dp(MAXN);intvis[MAXN];Nodeans;voidcal(intval,Node&x){if(x.mx<val){x.sec=x.mx,x.mx=val;}elseif(val!=x.mx&&x.sec<val){x.sec=val;}}voiddfs(intu){vis[u]=true;for(auto[v,w]:G[u]){if(vis[v]==1)continue;dfs(v);// ans 的轉移式cal(dp[v].mx+dp[u].mx+w,ans);cal(dp[v].mx+dp[u].sec+w,ans);cal(dp[u].mx+dp[v].sec+w,ans);// update dp[u]cal(dp[v].mx+w,dp[u]);cal(dp[v].sec+w,dp[u]);}}signedmain(){cin>>n;intu,v,w;for(inti=1;i<=n-1;i++){cin>>u>>v>>w;u++,v++;G[u].push_back({v,w});G[v].push_back({u,w});}dfs(1);cout<<ans.sec<<"\n";}