Cod sursa(job #2126449)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 9 februarie 2018 17:16:05
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>
#define MAXN 100000

std::vector <int> G[1 + MAXN];
int depth[1 + MAXN], best;
void dfs(int node, int fth){
    depth[node] = 1 + depth[fth];
    if(depth[node] > depth[best]) best = node;
    for(auto y:G[node]) if(y != fth) dfs(y, node);
}
int main(){
    FILE*fi,*fo;
    fi = fopen("darb.in","r");
    fo = fopen("darb.out","w");

    int n;
    fscanf(fi,"%d", &n);
    for(int i = 1; i < n; i++){
        int x, y;
        fscanf(fi,"%d%d", &x, &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1, 0);
    int root = best;
    best = 0;
    dfs(root, 0);
    fprintf(fo,"%d", depth[best]);
    return 0;
}