Cod sursa(job #2390164)

Utilizator alex.cojocaruAlex Cojocaru alex.cojocaru Data 27 martie 2019 20:12:59
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <vector>
#include <stdio.h>

#define NMAX 100000

using namespace std;

int best, depthmax ;
vector <int> g [ NMAX + 1 ] ;

void diam (int nod, int depth, int papa) {
  if (depth > depthmax ) {
    depthmax = depth ;
    best = nod ;
  }
  for (auto y : g[nod] ) {
    if (papa != y )
      diam(y, depth+1, nod) ;
  }
}

int main() {

  FILE *fin, *fout ;
  fin = fopen ("darb.in", "r" ) ;
  fout = fopen ("darb.out", "w" ) ;
  int n, i, a, b ;
  fscanf (fin, "%d", &n ) ;
  for (i = 0 ; i < n-1 ; i++ ) {
    fscanf (fin, "%d%d", &a, &b ) ;
    g[a].push_back(b) ;
    g[b].push_back(a) ;
  }
  best = 0 ;
  depthmax = 0 ;
  diam(1, 1, 0) ;
  diam(best, 1, 0) ;
  fprintf (fout, "%d", depthmax ) ;
  return 0;
}