Pagini recente » Cod sursa (job #1261253) | Cod sursa (job #1074984) | Cod sursa (job #2038208) | Statistici Ilca Dragos (dragos.ilca) | Cod sursa (job #1585405)
#include <fstream>
#include <vector>
using namespace std;
vector <int> a[100001];
int d[100001]; /// adancimea maxima din nodul i in jos
int lmax = 0;
void f(int nod, int tata);
int main() {
int n, x, y;
ifstream in("darb.in");
in >> n;
while (--n) {
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
f(1, -1);
ofstream out("darb.out");
out << lmax;
return 0;
}
void f(int nod, int tata) {
int l = 0, lsec = 0;
for (auto i : a[nod]) {
if (i == tata)
continue;
f(i, nod);
if (d[i] > l) {
lsec = l;
l = d[i];
}
else if (d[i] > lsec)
lsec = d[i];
}
if (l + lsec > lmax)
lmax = l + lsec + 1;
d[nod] = l + 1;
}