Cod sursa(job #2167765)

Utilizator Johnny07Savu Ioan-Daniel Johnny07 Data 13 martie 2018 23:24:35
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");

vector <int> a[100010];
int d1[100010],d2[100010];
int n;
int ma=0, poz;

void DFS (int node)
{
int i;
if (d1[node]>ma) {ma=d1[node];poz=node;}
    for (i=0;i<a[node].size();i++) if (d1[a[node][i]]==0)
    {

        d1[a[node][i]]=d1[node]+1;

        DFS(a[node][i]);
    }
}

void DFS2 (int node)
{
int i;
if (d2[node]>ma) {ma=d2[node];poz=node;}
    for (i=0;i<a[node].size();i++) if (d2[a[node][i]]==0)
    {

        d2[a[node][i]]=d2[node]+1;

        DFS2(a[node][i]);
    }
}


int main()
{
    int x,y,i;
    f>>n;
   for (i=1;i<=n;i++)
    {
        f>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    d1[1]=1;
    DFS(1);

    d2[poz]=1;
    ma=0;
    DFS2(poz);

    g<<ma;
    return 0;

}