Cod sursa(job #1611729)

Utilizator vladrochianVlad Rochian vladrochian Data 24 februarie 2016 13:11:20
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <vector>

using namespace std;

const int N_MAX = 1e5;

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

int N;
vector<int> G[N_MAX + 5];
pair<int, int> last;

void Dfs(int node, int padre, int level) {
   last = max(last, make_pair(level, node));

   for (int son : G[node])
      if (son != padre)
         Dfs(son, node, level + 1);
}

int main() {
   fin >> N;
   for (int i = 1; i < N; ++i) {
      int x, y;
      fin >> x >> y;
      G[x].push_back(y);
      G[y].push_back(x);
   }

   Dfs(1, 0, 1);
   int start = last.second;
   last = make_pair(0, 0);

   Dfs(start, 0, 1);
   fout << last.first << "\n";
   return 0;
}