Pagini recente » Cod sursa (job #2143510) | Cod sursa (job #498203) | Cod sursa (job #515388) | Cod sursa (job #634308) | Cod sursa (job #2698436)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
int n, nivelMaxim, raspuns;
int nivele[100005];
vector<int> mat[100005];
void dfs(int nodSunt, int nodVin) {
for (int i = 0; i < mat[nodSunt].size(); ++i) {
if (mat[nodSunt][i] != nodVin) {
nivele[mat[nodSunt][i]] = nivele[nodSunt] + 1;
dfs(mat[nodSunt][i], nodSunt);
}
}
if (mat[nodSunt].size() == 1) {
if (nivelMaxim < nivele[nodSunt]) {
nivelMaxim = nivele[nodSunt];
raspuns = nodSunt;
}
}
}
int main() {
fin >> n;
for (int i = 1; i < n; ++i) {
int x, y;
fin >> x >> y;
mat[x].push_back(y);
mat[y].push_back(x);
}
nivelMaxim = -1;
dfs(1, 0);
for (int i = 1; i <= n; ++i) {
nivele[i] = 0;
}
dfs(raspuns, 0);
fout << nivelMaxim + 1;
return 0;
}