Pagini recente » Cod sursa (job #731254) | Cod sursa (job #1135924) | Cod sursa (job #2965068) | Cod sursa (job #2931170) | Cod sursa (job #2816305)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("darb.in");
ofstream fout ("darb.out");
/**
Arbori
Def: Arborele este un graf conex si fara cicluri
Un arbore cu n noduri are exact n - 1 muchii
*/
vector <int> h[100005];
int n;
int d[100005], viz[100005];
void DFS (int x)
{
viz[x] = 1;
for (int i : h[x])
if (viz[i] == 0)
{
d[i] = d[x] + 1;
DFS(i);
}
}
int main()
{
int maxim = 0, nod_max;
fin >> n;
for (int i = 1; i <= n - 1; i++)
{
int x , y;
fin >> x >> y;
h[x].push_back(y);
h[y].push_back(x);
}
DFS(1);
for (int i = 1; i <= n; i++)
{
if (d[i] > maxim)
{
maxim = d[i];
nod_max = i;
}
d[i] = 0;
viz[i] = 0;
}
DFS(nod_max);
maxim = 0;
for (int i = 1; i <= n; i++)
maxim = max (maxim , d[i]);
fout << maxim + 1 << "\n";
return 0;
}