Pagini recente » Cod sursa (job #2186682) | Cod sursa (job #1116228) | Cod sursa (job #301578) | Cod sursa (job #112962) | Cod sursa (job #3002435)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
vector <int> G[100001];
int n;
int viz[100001];
int dist[100001];
void citire()
{
fin >> n;
int x, y;
for(int i = 0; i < n - 1; ++i)
{
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
}
int bfs(int x)
{
queue <int> q;
q.push(x);
viz[x] = 1;
int last = -1;
while(!q.empty())
{
int el = q.front();
last = el;
q.pop();
for(int i = 0; i < G[el].size(); ++i)
{
int to = G[el][i];
if(!viz[to])
{
q.push(to);
viz[to] = 1;
}
}
}
return last;
}
int distanta(int inc)
{
queue <int> q;
q.push(inc);
dist[inc] = 1;
int last = -1;
while(!q.empty())
{
int el = q.front();
last = el;
q.pop();
for(int i = 0; i < G[el].size(); ++i)
{
int to = G[el][i];
if(!dist[to])
{
q.push(to);
dist[to] = dist[el] + 1;
}
}
}
return dist[last];
}
int main()
{
citire();
int inc = bfs(1);
fout << distanta(inc);
return 0;
}