Cod sursa(job #1098622)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 4 februarie 2014 22:39:25
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <vector>
#define Nmax 100100
#define Vecin Graph[Nod][i]
using namespace std;

vector <int> Graph[Nmax];
int N, Solution, MiddleMan, Dist[Nmax];

void Dfs(int Nod, int Tata) {

    Dist[Nod] = Dist[Tata] + 1;

    if(Dist[Nod] > Dist[MiddleMan])
        MiddleMan = Nod;

    for(int i = 0; i < Graph[Nod].size(); i++)
        if(Vecin != Tata)
            Dfs(Vecin, Nod);

}
void Solve() {

    Dist[1] = 1;
    Dfs(1, 0);

    Dist[MiddleMan] = 1;
    Dfs(MiddleMan, 0);

    Solution = Dist[MiddleMan];

}
void Read() {

    int i, x, y;
    ifstream in("darb.in");
    in >> N;

    for(i = 1; i < N; i++) {
        in >> x >> y;
        Graph[x].push_back(y);
        Graph[y].push_back(x);
        }

    in.close();

}
void Write() {

    ofstream out("darb.out");
    out << Solution << '\n';
    out.close();

}
int main() {

    Read();
    Solve();
    Write();

    return 0;

}