Cod sursa(job #1997673)

Utilizator savigunFeleaga Dragos-George savigun Data 4 iulie 2017 23:58:43
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream in("darb.in");
ofstream out("darb.out");

int n, maxdepth, v;
vector<int> g[100005];

void dfs(int x, int parent, int depth) {
    if (depth > maxdepth) {
        maxdepth = depth;
        v = x;
    }
    for (int i = 0; i < g[x].size(); ++i) {
        int y = g[x][i];
        if (y == parent) continue;
        dfs(y, x, depth + 1);
    }
}

int main()
{
    in >> n;
    for (int i = 1, x, y; i < n; ++i) {
        in >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs(1, 1, 1);
    dfs(v, v, 1);
    out << maxdepth;


    return 0;
}