Pagini recente » Cod sursa (job #3347153) | Cod sursa (job #403105) | Cod sursa (job #647852) | Cod sursa (job #309043) | Cod sursa (job #3311488)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
const int nmax = 1e5;
int n, a, b, maxi, dp[nmax + 2];
int wheremx;
vector <int> muchii[nmax + 2];
void dfs(int node, int parent){
dp[node] = dp[parent] + 1;
maxi = max(maxi, dp[node]);
if(maxi == dp[node])
wheremx = node;
for(auto nxt : muchii[node]){
if(nxt != parent)
dfs(nxt, node);
}
}
int main(){
in>>n;
for(int i = 1; i <= n - 1; i++){
in>>a>>b;
muchii[a].push_back(b);
muchii[b].push_back(a);
}
dfs(1, 0);
dfs(wheremx, 0);
out<<maxi<<"\n";
return 0;
}