Cod sursa(job #2608874)

Utilizator Bogdan1999Draghici Bogdan Bogdan1999 Data 1 mai 2020 20:19:43
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("darb.in");
ofstream fout("darb.out");

vector <int> L[100009];
int n, i, sol, x, y, nodmax;

void dfs (int nod, int niv, int tata ){
    if (sol < niv){
        sol = niv;
        nodmax = nod;
    }
    for (unsigned int i = 0; i < L[nod].size(); i++)
        if (L[nod][i] != tata)
            dfs(L[nod][i], niv + 1, nod);
}

int main()
{
    fin >> n;
    for ( i=1; i < n; i++ ){
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    dfs(1, 1, 0);
    sol = 0;
    dfs (nodmax, 1, 0);
    fout << sol;
    return 0;
}