Pagini recente » Cod sursa (job #569686) | Cod sursa (job #2557438) | Cod sursa (job #1485661) | Cod sursa (job #865254) | Cod sursa (job #2450607)
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cmath>
#include <queue>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
vector<int>vp[100000];
bool viz[100000];
int dst[100000];
void bfs(int x)
{
int current = x;
queue<int> q;
q.push(x);
viz[x] = 1;
while (!q.empty())
{
current = q.front();
q.pop();
for (auto u : vp[current])
{
if (viz[u] == 0)
{
q.push(u);
viz[u] = 1;
dst[u] = dst[current] + 1;
}
}
}
}
int main()
{
int n;
fin >> n;
for (int i = 0; i < n - 1; i++)
{
int a, b;
fin >> a >> b;
vp[a].push_back(b);
vp[b].push_back(a);
}
int Max = 0, root = 0;
bfs(1);
for (int i = 1; i <= n; i++)
{
if (Max < dst[i])
{
Max = dst[i];
root = i;
}
}
for (int i = 1; i <= n; i++) viz[i] = 0, dst[i] = 0;
bfs(root);
Max = 0;
for (int i = 1; i <= n; i++)
Max = max(dst[i], Max);
fout << Max + 1;
}