Cod sursa(job #2551413)

Utilizator Alin_StanciuStanciu Alin Alin_Stanciu Data 19 februarie 2020 20:10:42
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int n, ma, T[100001], Dp[100001]; // Dp[x] - lantul maxim care porneste de la x si merge in jos pe arbore
vector<int> G[100001];

void Dfs(int x)
{
    Dp[x] = 1;
    for (int y : G[x])
    {
        if (y != T[x])
        {
            T[y] = x;
            Dfs(y);
            ma = max(ma, Dp[x] + Dp[y]);
            Dp[x] = max(Dp[x], Dp[y] + 1);
        }
    }
}

int main()
{
    fin >> n;
    for (int i = 1; i < n; ++i)
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    Dfs(1);
    fout << ma;

    return 0;
}