Cod sursa(job #3255794)

Utilizator SergiuS3003Sergiu Stancu Nicolae SergiuS3003 Data 12 noiembrie 2024 15:04:24
Problema Diametrul unui arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f ("darb.in");
ofstream g ("darb.out");
const int NMAX = 100005;
vector<int>G[NMAX];
int maxDepth = 0, maxLeaf = 0;
int DFS (int x, int father, int depth)
{
    if(depth > maxDepth)
    {
        maxDepth = depth;
        maxLeaf = x;
    }
    for(auto i:G[x])
    {
        if(i==father)
        {
            continue;
        }
        DFS(i,x,depth + 1);

    }
}
int main()
{
    int n;
    f >> n;
    for (int i = 1; i < n; i++)
    {
        int x, y;
        f >> x >> y;
        G[x].push_back (y);
        G[y].push_back (x);
    }
    DFS (1, 0, 0);
    DFS (maxLeaf, 0, 0);
    g << maxDepth + 1;
    return 0;
}