Cod sursa(job #2989845)

Utilizator iProgramInCppiProgramInCpp iProgramInCpp Data 7 martie 2023 09:03:13
Problema Diametrul unui arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define NMAX  100001
ifstream fin ("darb.in");
ofstream fout("darb.out");
int n;
vector<int> G[NMAX];
bool viz[NMAX];

int xmax, depmax;

void df(int x, bool vizch, int dep = 0)
{
    viz[x] = vizch;
    for (int i : G[x])
    {
        if (viz[i] != vizch)
            df(i, vizch, dep+1);
    }

    if (depmax < dep)
    {
        depmax = dep;
        xmax = x;
    }
}

int main()
{
    fin >> n;
    for (int i=1; i<n; i++)
    {
        int a,b;
        fin >>a >>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    df(1, true);
    int x = xmax;
    xmax = depmax = 0;
    df(x, false);

    cout << depmax + 1;

    return 0;
}