Cod sursa(job #2094914)
Utilizator | Visanu Cristian flibia | Data | 26 decembrie 2017 18:39:57 |
---|---|---|---|
Problema | Diametrul unui arbore | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.52 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
int n, m, mx, x, y;
vector <int> v[100100];
bool viz[100100];
void dfs(int from, int lvl){
if(lvl > mx){
mx = lvl;
x = from;
}
viz[from] = 1;
for(auto to : v[from])
if(!viz[to])
dfs(to, 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);
}
dfs(1, 1);
memset(viz, 0, sizeof viz);
dfs(x, 1);
out << mx;
return 0;
}