Cod sursa(job #2243523)

Utilizator CozmaCatalinCozma Catalin CozmaCatalin Data 20 septembrie 2018 19:43:03
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

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

const int LIM = 100000;

bool beenThere[LIM+1];
int theDistance[LIM+1];
vector < int > myVector[LIM+1];

int theFirstDFSMaxDistance;
int theSecondDFSMaxDistance;
int theFirstNode;

int N,M;

void DFS(int theNode , int theDistance)
{
    if(theDistance > theFirstDFSMaxDistance)
    {
        theFirstDFSMaxDistance = theDistance;
        theFirstNode = theNode;
    }
    beenThere[theNode] = true;
    for(auto V : myVector[theNode])
    {
        if(beenThere[V] == false)
        {
            DFS(V,theDistance+1);
        }
    }
}

int main()
{
    in >> N;
    for ( int i = 1; i < N ; ++i)
    {
        int x,y;
        in >> x >> y;
        myVector[x].push_back(y);
        myVector[y].push_back(x);
    }
    DFS(1,0);
    memset(beenThere,false,sizeof(beenThere));
    theFirstDFSMaxDistance = 0;
    DFS(theFirstNode,0);
    out << theFirstDFSMaxDistance + 1;
}