Cod sursa(job #1491374)

Utilizator Narcys01Ciovnicu Narcis Narcys01 Data 25 septembrie 2015 10:04:31
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

//queue <int> Q;
vector <int> a[100005];
int d[100005], n, L, Di;
bool v[100005];

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

void DFS(int k, int x)
{
    v[k] = true;
    int l = a[k].size();
    for (int i = 0; i < l; i++)
        if (!v[a[k][i]])
        {
            d[a[k][i]] = x + 1;
            if (d[a[k][i]] > Di)
            {
                L = a[k][i];
                Di = d[a[k][i]];
            }
            DFS(a[k][i], x + 1);
        }
    v[k] = false;
}

int main()
{
    int i, x, y;
    fin >> n;
    for (i = 1; i < n; i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    d[1] = 0;
    DFS(1, 0);
    DFS(L, 0);
    fout << Di + 1 << "\n";
    return 0;
}