Cod sursa(job #2351833)

Utilizator Cristian25Cristian Stanciu Cristian25 Data 22 februarie 2019 18:53:40
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <vector>
#include <queue>
#define len 100010
#define pb push_back

using namespace std;

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

vector<unsigned> v[len];

queue<unsigned> q;

unsigned N, last, lung[len];
bool viz[len];

void bfs(unsigned nod)
{
    q.push(nod);
    lung[nod] = 1;
    viz[nod] = true;
    while(!q.empty())
    {
        unsigned el = q.front();
        for(unsigned k = 0; k < v[el].size(); ++k)
            if(!viz[v[el][k]])
            {
                viz[v[el][k]] = true;
                q.push(v[el][k]);
                lung[v[el][k]] = lung[el] + 1;
            }
        last = el;
        q.pop();
    }
}

int main()
{
    in >> N;
    for(unsigned i = 1; i < N; ++i)
    {
        unsigned x, y;
        in >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    bfs(1);
    for(unsigned i = 1; i <= N; ++i)
    {
        viz[i] = false;
        lung[i] = 0;
    }
    bfs(last);
    out << lung[last];
    return 0;
}