Pagini recente » Cod sursa (job #1298379) | Cod sursa (job #867631) | Cod sursa (job #1572484) | Cod sursa (job #1204845) | Cod sursa (job #2147787)
#include <bits/stdc++.h>
#define nmax 100007
using namespace std;
ifstream fin ("darb.in");
ofstream fout ("darb.out");
int n, viz[nmax], d[nmax];
vector <int> L[nmax];
queue <int> q;
void Citire()
{
int x, y;
fin >> n;
for (int i = 1; i <= n; i++)
{
fin >> x >> y;
L[x].push_back(y);
L[y].push_back(x);
}
}
void BFS(int nod)
{
int k;
d[nod] = 1;
viz[nod] = 1;
q.push(nod);
while (!q.empty())
{
k = q.front();
q.pop();
for (auto i : L[k])
if (!viz[i])
{
q.push(i);
viz[i] = 1;
d[i] = d[k] + 1;
}
}
}
int main()
{
int i, nod1, nod2;
Citire();
BFS(1);
nod1 = 2;
nod2 = 1;
for (i = 3; i <= n; i++)
if (d[i] > d[nod1])
{
nod2 = nod1;
nod1 = i;
}
else if (d[i] > d[nod2])
nod2 = 2;
fout << d[nod1] + d[nod2] - 1 << "\n";
fin.close();
fout.close();
return 0;
}