Cod sursa(job #2204990)

Utilizator andreistanStan Andrei andreistan Data 17 mai 2018 16:08:56
Problema Diametrul unui arbore Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("Darb.in");
ofstream g("Darb.out");

vector<int> v[27041];
int N, nodmax, maxlg;
bool viz[27041];

void DFS(int x, int lg)
{
    viz[x] = 1;
    if(maxlg < lg)
    {
        maxlg = lg;
        nodmax = x;
    }
    for(unsigned int i = 0; i < v[x].size(); i++)
        if(viz[v[x][i]] == 0)
            DFS(v[x][i], lg + 1);
}

int main()
{
    int x,y;
    f >> N;
    for(int i = 1; i < N; i++)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    DFS(1,1);
    for(int i=1;i<=N;i++)
        viz[i]=0;
    maxlg=0;
    DFS(nodmax,1);
    g<<maxlg;
    return 0;
}