Pagini recente » Cod sursa (job #286433) | Cod sursa (job #449234) | Istoria paginii runda/olivranceanu | Cod sursa (job #449369) | Cod sursa (job #2678811)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("darbin.in");
ofstream fout("darbin.out");
queue < pair <int, int > > q;
vector <int > graph[100005];
int lg[100005] ,n;
void init()
{
for(int i = 1; i <= n; i++)
lg[i] = -1;
}
void citire()
{
fin >> n;
init();
for(int i = 0; i < n - 1; i++)
{
int x,y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
void bfs(int st){
lg[st] = 0;
q.push({st, 0});
while(!q.empty()){
int vf = q.front().first;
int lc = q.front().second + 1;
for(auto &v : graph[vf])
if(lg[v] == -1){
q.push({v, lc});
lg[v] = lc;
}
q.pop();
}
}
int cautmax()
{
int maxx = 0,nod;
for(int i = 1; i <= n; i++)
if(lg[i] > maxx)
maxx = lg[i],nod = i;
return nod;
}
int main()
{
citire();
bfs(1);
int maxi = cautmax();
init();
bfs(maxi);
fout <<lg[cautmax()] + 1;
return 0;
}