Cod sursa(job #2128053)

Utilizator DanizisSpartanulDani Mocanu DanizisSpartanul Data 11 februarie 2018 13:34:30
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

constexpr int NMax=100005;

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

int N;
vector<int> G[NMax];
int length,lastNode;

void DFS(int node, int father, int level)
{
    if(level>length)
        length=level, lastNode=node;
    for(auto child : G[node])
        if(child!=father)
            DFS(child,node,level+1);
}

int main()
{
    fin>>N;
    for(int i=1;i<N;i++)
    {
        int x,y;
        fin>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFS(1,0,1);
    length=0; DFS(lastNode,0,1);
    fout<<length;
    return 0;
}