Cod sursa(job #2204999)

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

vector<int> v[100001];
int N, nodmax;
bool viz[100001];
int lg[100001];
queue<int> Coada;

void BFS(int x)
{
    Coada.push(x);
    viz[x] = 1;
    while(!Coada.empty())
    {
        int crt = Coada.front();
        Coada.pop();
        for(auto &it : v[crt])
            if(viz[it] == 0)
            {
                Coada.push(it);
                viz[it] = 1;
                lg[it] = 1 + lg[crt];
                nodmax = it;
            }
    }
}

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);
    }
    BFS(1);
    for(int i = 1; i <= N; i++)
        viz[i] = 0,lg[i]=1;
    BFS(nodmax);
    g << lg[nodmax];
    return 0;
}