Cod sursa(job #2425268)
Utilizator | Data | 24 mai 2019 17:44:04 | |
---|---|---|---|
Problema | Diametrul unui arbore | Scor | 10 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.53 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
int n, m, x, y, viz[100100];
vector<int> v[100100];
void dfs(int f, int lvl) {
viz[x] = 1;
if (lvl > y) {
y = lvl;
x = f;
}
for (auto g : v[f])
if (!viz[g])
dfs(g, lvl + 1);
}
int main() {
in >> n;
for (int i = 1; i < n; i++) {
in >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
y = 0;
dfs(1, 1);
memset(viz, 0, sizeof viz);
y = 0;
dfs(x, 1);
out << y - 1;
return 0;
}