Pagini recente » Cod sursa (job #2108209) | Cod sursa (job #919667) | Cod sursa (job #1617000) | Cod sursa (job #1410824) | Cod sursa (job #2697013)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
#defin MAXSIZE 10010
vector<int> graf[MAXSIZE];
int dist[MAXSIZE];
void bfs(int node)
{
dist[node] = 1;
queue<int> q;
q.push(node);
while(!q.empty())
{
node = q.front();
q.pop();
for(int i = 0; i < (int)graf[node].size(); i++)
{
int next = graf[node][i];
if(!dist[next])
{
dist[next] = dist[node] + 1;
q.push(next);
}
}
}
}
int main()
{
int n;
f >> n;
for(int i = 1; i <= n - 1; i++)
{
int x, y;
f >> x >> y;
graf[x].push_back(y);
graf[y].push_back(x);
}
bfs(1);
int maximum = 0;
int node = 1;
for(int i = 1; i <= n; i++)
{
if(dist[i] > maximum)
{
maximum = dist[i];
node = i;
}
}
for(int i = 1; i <= n; i++)
{
dist[i] = 0;
}
bfs(node);
maximum = 0;
for(int i = 1; i <= n; i++)
{
maximum = max(maximum, dist[i]);
}
g << maximum;
return 0;
}