Cod sursa(job #2425735)

Utilizator ifrimencoAlexandru Ifrimenco ifrimenco Data 25 mai 2019 00:22:00
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
vector <int> v[100002];
vector <int> Viz(100002, 0);

int Mata = 0, Tactu = 0;
void FrunzaVerde(int x, int Dist) {
    Viz[x] = 1;
    if (Mata <= Dist) {
        Mata = Dist;
        Tactu = x;
    }
    for (int i = 0; i < v[x].size(); i++) {
        if (!Viz[v[x][i]])
            FrunzaVerde(v[x][i], Dist + 1);
    }
}

int DistMax = 0;
void DFS(int x, int Dist) {
    Viz[x] = 1;
    DistMax = max(DistMax, Dist);
    for (int i = 0; i < v[x].size(); ++i) {
        if (!Viz[v[x][i]])
            DFS(v[x][i], Dist + 1);
    }
}

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

int main()
{
    int n, m, i;
    fin >> n;
    for (i = 0; i < n - 1; ++i) {
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    FrunzaVerde(1, 0);
    fill(Viz.begin(), Viz.end(), 0);
    DFS(Tactu, 1);
    fout << DistMax << '\n';
    return 0;
}