Pagini recente » Cod sursa (job #1586374) | Cod sursa (job #1520873) | Cod sursa (job #569974) | Cod sursa (job #2615434) | Cod sursa (job #3285760)
#include <fstream>
#include <queue>
using namespace std;
ifstream cin ("darb.in");
ofstream cout ("darb.out");
const int N = 1e5;
int dist[N + 1];
vector <int> g[N + 1];
int n, x, y, frunza, mx;
void bfs (int node)
{
for (int i = 1; i <= n; ++i)
dist[i] = 0;
queue <int> q;
q.push(node);
dist[node] = 1;
mx = 0;
while (!q.empty())
{
int x = q.front();
if (mx < dist[x])
mx = dist[x], frunza = x;
q.pop();
for (auto it : g[x])
if (!dist[it])
dist[it] = dist[x] + 1, q.push(it);
}
}
int main()
{
cin >> n;
for (int i = 1; i < n; ++i)
{
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
bfs (1);
bfs (frunza);
cout << mx << '\n';
return 0;
}