Pagini recente » Cod sursa (job #2986175) | Cod sursa (job #1542391) | Cod sursa (job #1396880) | Cod sursa (job #997524) | Cod sursa (job #2114568)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("darb.in");
ofstream fout ("darb.out");
const int Nmax=100005;
int N, dmax, dist[Nmax], ultimul;
vector < int > listaAdiacenta[Nmax];
queue < int > coada;
void Read()
{
fin >> N;
for (int i=1; i<=N-1; i++)
{
int x, y;
fin >> x >> y;
listaAdiacenta[x].push_back(y);
listaAdiacenta[y].push_back(x);
}
fin.close();
}
void BFS(int nod)
{
coada.push(nod);
for (int i=1; i<=Nmax; i++)
dist[i]=0;
dist[nod]=1;
while (!coada.empty())
{
int nodCurent=coada.front();
coada.pop();
for (int i=0; i<listaAdiacenta[nodCurent].size(); i++)
{
if (dist[listaAdiacenta[nodCurent][i]]==0)
{
dist[listaAdiacenta[nodCurent][i]]=dist[nodCurent]+1;
coada.push(listaAdiacenta[nodCurent][i]);
dmax=dist[listaAdiacenta[nodCurent][i]];
ultimul=listaAdiacenta[nodCurent][i];
}
}
}
}
void Rezolvare()
{
BFS(1);
BFS(ultimul);
}
void Print()
{
fout << dmax << "\n";
fout.close();
}
int main()
{
Read();
Rezolvare();
Print();
}