Pagini recente » Cod sursa (job #3276476) | Cod sursa (job #3122564) | Cod sursa (job #181842) | Cod sursa (job #3285898) | Cod sursa (job #3250079)
#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 d[100005];
int dfs(int nod) {
if (d[nod] > d_max) {
fr_max = nod;
d_max = d[nod];
}
viz[nod] = 1;
for (int vec: v[nod]) {
if (!viz[vec]) {
d[vec] = d[nod] + 1;
dfs(vec);
}
}
}
int main() {
f >> n;
for (int i=1;i<n;i++) {
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
d[1] = 1;
dfs(1);
for (int i=1;i<=n;i++) {
viz[i] = 0;
d[i] = 0;
}
d[fr_max] = 1;
d_max = 0;
dfs(fr_max);
g << d_max;
return 0;
}