Pagini recente » Cod sursa (job #1200415) | Cod sursa (job #528132) | Cod sursa (job #3003872) | Cod sursa (job #1113587) | Cod sursa (job #1250793)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
const int LIM = 1000005;
int N;
int Last, answer;
int Dist[LIM];
bool beenThere[LIM];
vector < int > V[LIM];
queue < int > qu;
void BFS(int n)
{
int number;
for(int i = 0; i < LIM; i++)
{
Dist[i] = 0;
beenThere[i] = false;
}
qu.push(n);
Dist[n] = 1;
beenThere[n] = true;
while(!qu.empty())
{
number = qu.front();
for(int i = 0; i < V[number].size(); i++)
{
if(!beenThere[V[number][i]])
{
qu.push(V[number][i]);
Dist[V[number][i]] = Dist[number] + 1;
beenThere[V[number][i]] = true;
answer = Dist[V[number][i]];
Last = V[number][i];
}
}
qu.pop();
}
}
void Read()
{
int x, y;
fin >> N;
for(int i = 1; i <= N - 1; i++)
{
fin >> x >> y;
V[x].push_back(y);
V[y].push_back(x);
}
}
int main()
{
Read();
BFS(1);
cout << Last;
BFS(Last);
fout << answer;
return 0;
}