Pagini recente » Cod sursa (job #1245822) | Cod sursa (job #3268563) | Cod sursa (job #2094434) | Cod sursa (job #107703) | Cod sursa (job #2769370)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("darb.in");
ofstream fout ("darb.out");
const int maxVal = 100001;
struct nod
{
int info;
nod * urm;
}* v[maxVal];
void adaugare (int x, int y)
{
nod * t = new nod;
t -> info = y;
t -> urm = v[x];
v[x] = t;
}
void dfs (int x, int h, int &hMax, int vizitat[], int &nodMax)
{
vizitat[x] = 1;
nod * t = v[x];
while (t)
{
if (vizitat[t -> info] == 0)
dfs (t -> info, h+1, hMax, vizitat, nodMax);
t = t -> urm;
}
if (h > hMax)
{
hMax = h;
nodMax = x;
}
}
int main()
{
int n, x, y;
fin >> n;
while (fin >> x >> y)
{
adaugare (x, y);
adaugare (y, x);
}
int hMax = 0;
int vizitat[maxVal] = {0};
int nodMax;
dfs(1, 1, hMax, vizitat, nodMax);
for (int i = 1; i <= n; i++)
vizitat[i] = 0;
hMax = 0;
dfs(nodMax, 1, hMax, vizitat, nodMax);
fout << hMax;
return 0;
}