Cod sursa(job #2300299)

Utilizator vladisimovlad coneschi vladisimo Data 11 decembrie 2018 09:40:13
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <cstdio>
#include <vector>

int uFar, max;

std::vector<int> G[2 + 100000];

bool visited[2 + 100000];

void dfs(int u, int dist) {
  if (dist > max) {
    max = dist;
    uFar = u;
  }
  visited[u] = true;
  for (int v : G[u])
    if (!visited[v])
      dfs(v, dist + 1);
}

int main() {
  freopen("darb.in", "r", stdin);
  freopen("darb.out", "w", stdout);
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++)
    visited[i] = false;
  dfs(uFar, 1);
  printf("%d", max);
  return 0;
}