Cod sursa(job #1368518)

Utilizator Miruna_DMiruna Miruna_D Data 2 martie 2015 18:16:59
Problema Diametrul unui arbore Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <vector>
#define Nmax 100001
#define oo 1000000000
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
vector<int> G[Nmax];
int use[Nmax];
int max1,max2;
int n;

void read()
{
      fin>>n;
      int i;
    for(i=1;i<n;i++)
    {
        int a,b;
        fin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
}

void DFS(int nod,int depth)
{
    use[nod]=1;
    for(unsigned int i=0;i<G[nod].size();i++)
        if(!use[G[nod][i]])
        {
            DFS(G[nod][i],depth+1);
        }
    int d=depth;
    if(depth>=max1)
        max2=max1,max1=depth;
    else
        if(depth>max2)
                max2=depth;

}
int main()
{
    read();
    max1=-oo,max2=-oo;
    DFS(1,1);
    fout<<max1+max2-1;
    return 0;
}