Cod sursa(job #2179977)

Utilizator gundorfMoldovan George gundorf Data 20 martie 2018 15:57:30
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
#define Nmax 100002
#include <vector>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");

int viz[Nmax];
int dist[Nmax];
int distanta_maxima;
vector <int> v[Nmax];
int n,m;

void DFS (int x,int t,int &dmax) //dmax e nodul de distanta maxima fata de varf
{
    int i;
    viz[x]=1;
    dist[x]=dist[t]+1;

    if (dist[x]>distanta_maxima)
    {
        distanta_maxima=dist[x];
        dmax=x;
    }

    for (int it:v[x])
        {
            if ( viz[it]==0)
            DFS (it,x,dmax);
        }
}

void Citire ()
{
    int i,x,y;
    fin>>n;
    m=n-1;
    for (i=1; i<=m; i++)
    {
        fin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
}

int main()
{
    int dmax,i;
    Citire();
    DFS(1,0,dmax);
    for (i=1; i<=n; i++)
        viz[i]=dist[i]=0;
    distanta_maxima=0;
    DFS(dmax,0,dmax);
    fout<<distanta_maxima;
    return 0;
}