#include using namespace std; using LL = long long; const LL LINF = 1e18; class Edge{ public: int to,from,value; Edge(int t,int f,int c){ to = t; from = f; value = c; } }; #define P pair std::vector dijkstra(int s,int N,vector E){ std::vector ans(N,LINF); std::vector> Edges(N); for(auto e : E){ Edges.at(e.from).push_back(e); } priority_queue, greater

> que; ans.at(s)= 0; que.push(make_pair(0,s)); while(!que.empty()){ P p = que.top();que.pop(); if(ans.at(p.second) < p.first)continue; for(int i = 0;i < Edges.at(p.second).size();i++){ if(ans.at(p.second) + Edges.at(p.second).at(i).value < ans.at(Edges.at(p.second).at(i).to)){ ans.at(Edges.at(p.second).at(i).to) = ans.at(p.second) + Edges.at(p.second).at(i).value; que.push(make_pair(ans.at(Edges.at(p.second).at(i).to),Edges.at(p.second).at(i).to)); } } } return ans; } int main(){ int N;cin >> N; vector Edges; for(int a = 0;a < N-1;a++){ int b,c;cin >> b >> c; Edges.push_back(Edge(b-1,c-1,1)); Edges.push_back(Edge(c-1,b-1,1)); } vector di = dijkstra(0,N,Edges); int idx = max_element(di.begin(),di.end())-di.begin(); vector ansns = dijkstra(idx,N,Edges); cout<