Cod sursa(job #1700351)

Utilizator BrandonChris Luntraru Brandon Data 10 mai 2016 11:19:08
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

const int MaxN = 100005;

vector <int> G[MaxN];

bool used[MaxN];
int n, farthest, MaxLevel;

void Dfs(int node, int level = 1) {
  used[node] = true;

  if (level > MaxLevel) {
    MaxLevel = level;
    farthest = node;
  }

  for (auto nxt: G[node]) {
    if (used[nxt]) {
      continue;
    }

    Dfs(nxt, level + 1);
  }
}

int main() {
  cin >> n;

  for (int i = 1; i <= n - 1; ++i) {
    int a, b;
    cin >> a >> b;

    G[a].push_back(b);
    G[b].push_back(a);
  }

  Dfs(1);
  MaxLevel = 0;
  memset(used, 0, sizeof used);
  Dfs(farthest);
  cout << MaxLevel << '\n';
  return 0;
}