Pagini recente » Cod sursa (job #94155) | Cod sursa (job #3296533) | Cod sursa (job #3293712) | Cod sursa (job #2665867) | Cod sursa (job #3297535)
#include <bits/stdc++.h>
#define NMAX 100000
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
int n, a, b;
int nod_1, nod_2, dist_max;
vector<int> arb[NMAX + 2];
vector<int> dist(NMAX + 2, 0), dist_2(NMAX + 2, 0);
void BFS(int start, vector<int> &d, int &nod_term) {
queue<int> q;
q.push(start);
d[start] = 1;
while (!q.empty()) {
int nod = q.front();
q.pop();
for (int vec : arb[nod]) {
if (!d[vec]) {
d[vec] = d[nod] + 1;
if (d[vec] > dist_max) {
dist_max = d[vec];
nod_term = vec;
}
q.push(vec);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
fin >> n;
for (int i = 1; i <= n - 1; i++) {
fin >> a >> b;
arb[a].emplace_back(b);
arb[b].emplace_back(a);
}
BFS(1, dist, nod_1);
dist_max = 0;
BFS(nod_1, dist_2, nod_2);
fout << dist_max;
return 0;
}