Cod sursa(job #1984323)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 24 mai 2017 15:29:49
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

const int nMax = 100005;

int n;
vector<int> vecini[nMax];
bool viz[nMax];
int maxLength, startNode;

void citire()
{
    ifstream in("darb.in");
    in >> n;
    int x, y;
    for(int i = 1; i < n; ++i)
    {
        in >> x >> y;
        vecini[x].push_back(y);
        vecini[y].push_back(x);
    }
}

void Dfs(int current, int length)
{
    if(length > maxLength)
    {
        maxLength = length;
        startNode = current;
    }
    viz[current] = true;
    for(auto v:vecini[current])
    {
        if(viz[v] == false)
            Dfs(v, length + 1);
    }
}

void rezolvare()
{
    Dfs(1, 1);
    memset(viz, 0, sizeof(viz));
    Dfs(startNode, 1);
    ofstream out("darb.out");
    out << maxLength;
    out.close();
}

int main()
{
    citire();
    rezolvare();
    return 0;
}