Cod sursa(job #1379031)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 6 martie 2015 15:49:50
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define MAXN 100010

FILE *in = fopen("darb.in", "r");
FILE *out = fopen("darb.out", "w");

int n, x, y;
int maxNod, maxDist[MAXN];
vector<int> vec[MAXN];
bitset<MAXN> viz;

void BFS(int nod, int maxd) {
    queue<int> Q;
    Q.push(nod);
    maxDist[nod] = 1;
    viz.reset();
    viz[nod] = 1;

    while(!Q.empty()) {
        nod = Q.front(); Q.pop();
        if(!maxd)
            ;//cout << nod << " ";
        maxNod = nod;
        for(auto it: vec[nod]) {
            if(!viz[it]) {
                //cout << nod << "-" << it << "\n";
                Q.push(it);
                viz[it] = 1;
                if(maxd)
                    maxDist[it] = maxDist[nod] + 1;
            }
        }
    }
}

int main() {
    fscanf(in, "%d", &n);
    for(int i = 1; i < n; ++i) {
        fscanf(in, "%d%d", &x, &y);
        vec[x].pub(y);
        vec[y].pub(x);
    }

    BFS(1, 0);
    BFS(maxNod, 1);
    fprintf(out, "%d\n", maxDist[maxNod]);
    return 0;
}