Pagini recente » Cod sursa (job #1750048) | Cod sursa (job #668796) | Cod sursa (job #943125) | Cod sursa (job #2534273) | Cod sursa (job #2479727)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 100005
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
queue<int> q;
vector<int> v[NMAX];
int n;
bool ap[NMAX];
int dist[NMAX];
void BFS(int nod)
{
ap[nod]=true;
dist[nod]=0;
q.push(nod);
while(!q.empty()){
int fata=q.front();
q.pop();
for(auto i:v[fata]){
if(!ap[i]){
q.push(i);
ap[i]=true;
dist[i]=dist[fata]+1;
}
}
}
}
int main()
{
int x,y;
in>>n;
for(int i=1;i<n;++i){
in>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
BFS(1);
int maxim=-1,indice;
for(int i=1;i<=n;++i)
if(dist[i]>maxim){
maxim=dist[i];
indice=i;
}
for(int i=1;i<=n;++i){
dist[i]=0;
ap[i]=false;
}
BFS(indice);
maxim=-1;
for(int i=1;i<=n;++i)
if(dist[i]>maxim){
maxim=dist[i];
indice=i;
}
out<<maxim+1;
return 0;
}