Pagini recente » Cod sursa (job #2900583) | Cod sursa (job #501230) | Cod sursa (job #219185) | Cod sursa (job #440741) | Cod sursa (job #3152901)
#include <bits/stdc++.h>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
int n, x, y, nmax;
vector<int> adj[100005];
int d[100005];
void bfs(int nod) {
for (int i = 1; i <= n; i++)
d[i] = 1e9;
d[nod] = 1;
queue<int> q;
q.push(nod);
while (!q.empty()) {
int nod = q.front();
q.pop();
for (auto it : adj[nod])
if (d[it] > d[nod] + 1) {
d[it] = d[nod] + 1;
q.push(it);
}
}
}
int main()
{
in >> n;
for (int i = 1; i < n; i++) {
in >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
bfs(1);
nmax = -1;
for (int i = 1; i <= n; i++)
if (nmax == -1 || d[i] > d[nmax]) {
nmax = i;
}
bfs(nmax);
int dmax = 0;
for (int i = 1; i <= n; i++)
dmax = max(dmax, d[i]);
out << dmax << '\n';
return 0;
}