Pagini recente » Cod sursa (job #646012) | Cod sursa (job #646016) | Cod sursa (job #3203287) | Cod sursa (job #3285902) | Cod sursa (job #3250067)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
int n, x, y;
vector < int > v[100005];
int fr_max, d_max;
bool viz[100005];
int dfs(int nod, int d) {
if (d > d_max) {
fr_max = nod;
d_max = d;
}
viz[nod] = 1;
for (int vec: v[nod]) {
if (!viz[vec]) {
dfs(vec, d+1);
}
}
}
int main() {
f >> n;
for (int i=1;i<n;i++) {
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 0);
fill(viz+1, viz+n+1, 0);
d_max = 0;
dfs(fr_max, 0);
g << d_max + 1;
return 0;
}