Pagini recente » Cod sursa (job #1539613) | Cod sursa (job #1911450) | Cod sursa (job #576247) | Cod sursa (job #61074) | Cod sursa (job #2575534)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
const int maxn = 100005;
vector <int> v[maxn];
int dist[maxn];
int n;
void bfs(int nod)
{
for(int i = 1; i <= n; i++)
dist[i] = (1 << 30);
dist[nod] = 0;
queue <int> q;
q.push(nod);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto it : v[nod])
{
if(dist[it] > dist[nod] + 1)
{
dist[it] = dist[nod] + 1;
q.push(it);
}
}
}
}
int main()
{
in >> n;
for(int i = 1; i < n; i++)
{
int x, y;
in >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
bfs(1);
int nod = 0;
int mx = 0;
for(int i = 1; i <= n; i++)
{
if(dist[i] > mx)
{
nod = i;
mx = dist[nod];
}
}
bfs(nod);
mx = 0;
for(int i = 1; i <= n; i++)
mx = max(mx, dist[i]);
out << mx + 1 << "\n";
return 0;
}