Pagini recente » Cod sursa (job #3199421) | Cod sursa (job #3288102) | Cod sursa (job #3121809) | Cod sursa (job #3285828) | Cod sursa (job #3250082)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
int n, x, y;
vector < int > v[10005];
int fr_max, d_max;
bool viz[10005];
void 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, 1);
for (int i=1;i<=n;i++)
viz[i] = 0;
d_max = 0;
dfs(fr_max, 1);
g << d_max;
return 0;
}