Pagini recente » Cod sursa (job #1279365) | Cod sursa (job #583708) | Cod sursa (job #1219797) | Cod sursa (job #2526066) | Cod sursa (job #2541120)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n, l1[100003], l2[100003], maxi, nod;
vector<int> graph[100005];
queue<int> Q;
ifstream f("darb.in");
ofstream g("darb.out");
void part1() {
Q.push(1);
l1[1] = 1;
maxi = 1;
nod = 1;
while (!Q.empty()) {
int node = Q.front();
Q.pop();
for (auto &v:graph[node])
if (!l1[v]) {
l1[v] = l1[node] + 1;
Q.push(v);
if (maxi < l1[v]) {
maxi = l1[v];
nod = v;
}
}
}
}
void part2() {
Q.push(nod);
l2[nod] = 1;
maxi = 1;
while (!Q.empty()) {
int node = Q.front();
Q.pop();
for (auto &v:graph[node])
if (!l2[v]) {
l2[v] = l2[node] + 1;
Q.push(v);
maxi = max(l2[v], maxi);
}
}
}
int main() {
f >> n;
int x, y;
for (int i = 0; i < n - 1; i++) {
f >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
part1();
part2();
g << maxi;
return 0;
}