Cod sursa(job #2201905)

Utilizator herminamateiMatei Hermina herminamatei Data 6 mai 2018 14:41:27
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
vector<int> viz;
vector<vector<int>> G;
int n, mx=0;
void rd()
{
    int x, y;
    f>>n;
    G.resize(n+1);
    viz.resize(n+1);
    while(f>>x>>y)
    {
        G[x].push_back(y);
        G[y].push_back(x);
    }

}

void DFS(int x)
{
    for(int i=0;i<G[x].size();i++)
        if(!viz[G[x][i]])
    {
        viz[G[x][i]]=viz[x]+1;
        if(viz[G[x][i]]>viz[mx])
            mx=G[x][i];
        DFS(G[x][i]);
    }
}
int main()
{
    rd();
    viz[1]=1;
    DFS(1);
    viz.assign(n+1, 0);
    viz[mx]=1;
    DFS(mx);
    g<<viz[mx];
    return 0;
}