Cod sursa(job #1817217)

Utilizator Vlad_317Vlad Panait Vlad_317 Data 27 noiembrie 2016 15:00:45
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

const int MAX = 100000;

vector<int> g[MAX + 1];
bool viz[MAX + 1];
int lmax, nmax;

void dfs(int nod, int level)
{
    viz[nod] = 1;
    if(level > lmax)
    {
        lmax = level;
        nmax = nod;
    }
    for(int i = 0; i < g[nod].size(); i++)
    {
        if(viz[g[nod][i]] == 0)
            dfs(g[nod][i], level + 1);
    }
}

int main()
{
    FILE *fin, *fout;

    fin = fopen("darb.in", "r");
    fout = fopen("darb.out", "w");

    int n;

    fscanf(fin, "%d", &n);

    for(int i = 1; i < n; i++)
    {
        int x, y;
        fscanf(fin, "%d%d", &x, &y);
        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs(1, 0);
    memset(viz, 0, sizeof(viz));
    lmax = 0;
    dfs(nmax, 0);

    fprintf(fout, "%d", lmax + 1);

    return 0;
}