Cod sursa(job #1598404)

Utilizator AntoniooMacovei Antonio Antonioo Data 12 februarie 2016 21:24:38
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <stdio.h>
#include <vector>
using namespace std;
vector <int> v[100001];
int viz[100001], dist[100001], maxval, maxpoz;
void dfs(int nod) {
    viz[nod]=1; int i;
    for(i=0;i<v[nod].size();i++) {
        if(!viz[v[nod][i]]) {
            dist[v[nod][i]] = dist[nod] + 1;
            if(dist[v[nod][i]] > maxval) {
                maxval = dist[v[nod][i]];
                maxpoz = v[nod][i];
            }
            dfs(v[nod][i]);
        }
    }
}
int main()
{
    freopen("darb.in","r",stdin);
    freopen("darb.out","w",stdout);
    int n, i,x , y;
    scanf("%d",&n);
    for(i=1;i<=n;i++) {
        scanf("%d%d",&x,&y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1);

    maxval = 0;
    for(i=1;i<=n;i++) {
        dist[i]=0;
        viz[i]=0;
    }

    dfs(maxpoz);
    printf("%d",dist[maxpoz]+1);
    return 0;
}