Cod sursa(job #1827790)

Utilizator cubaLuceafarul cuba Data 12 decembrie 2016 12:47:07
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
const int nMax = 100002;
vector <int> Tree[nMax];
int dp[nMax], start;
inline void Dfs(int nod) {
    if(dp[nod] > dp[start]) {
        start = nod;
    }
    for(auto i : Tree[nod]) {
        if(!dp[i]) {
            dp[i] = dp[nod] + 1;
            Dfs(i);
        }
    }
}
int main()
{
    int n, x, y;
    f >> n;
    for(int i = 1; i < n; i++) {
        f >> x >> y;
        Tree[x].push_back(y);
        Tree[y].push_back(x);
    }
    dp[1] = 1;
    Dfs(1);
    for(int i = 1; i <= n; i++) {
        dp[i] = 0;
    }
    dp[start] = 1;
    Dfs(start);
    g<< dp[start] << "\n";
    return 0;
}