Cod sursa(job #1095477)

Utilizator FlameingoAiordachioaei Marius Flameingo Data 31 ianuarie 2014 10:00:45
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <cstdio>
#include <vector>
#include <bitset>
using namespace std;

const int NMAX = 100002;

bitset <NMAX> vis;
vector <int> T[NMAX];
int max_depth, final_node;

void DFS (const int &node, const int &depth) {

    vis[node] = 1;
    if (max_depth < depth) {
        final_node = node;
        max_depth = depth;
    }
    for (int i = 0; i < T[node].size (); ++i)
        if (!vis[T[node][i]])
            DFS (T[node][i], depth + 1);

}

int main () {

    freopen ("darb.in", "r", stdin);
    freopen ("darb.out", "w", stdout);
    int N, i, a, b;
    scanf ("%d", &N);
    for (i = 1; i < N; ++i) {
        scanf ("%d%d", &a, &b);
        T[a].push_back (b);
        T[b].push_back (a);
    }
    DFS (1, 1);
    vis.reset ();
    max_depth = 0;
    DFS (final_node, 1);
    printf ("%d", max_depth);

}