Cod sursa(job #3207420)

Utilizator BoggiGurau Bogdan Boggi Data 26 februarie 2024 10:23:40
Problema Diametrul unui arbore Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

ifstream fin("darb.in");
ofstream fout("darb.out");

#define pb push_back
#define PI pair<int, int>
#define VB vector<bool>
#define VP vector<PI>
#define VI vector<int>
#define VVI vector<VI>

VVI graph;
VB vis;
int n, m, diam = 0;

void DFS(int nod, int dist) {
    for (auto e : graph[nod]) {
        if (!vis[e]) {
            vis[e] = true;
            DFS(e, dist + 1);
        }
    }
    //cout << dist << ' ';
    diam = max(diam, dist);
}

void printGraph() {
    for (int i = 1; i <= n; ++i) {
        fout << i << ": ";
        for(auto e: graph[i]) {
            fout << e << ' ';
        }
        fout << '\n';
    }
}

int main() {    
    fin >> n;
    graph = VVI(n + 1);
    for (int i = 1; i < n; ++i) {
        int x, y;
        fin >> x >> y;
        graph[x].pb(y);
        graph[y].pb(x);
    }

    //printGraph();

    for (int i = 1; i <= n; ++i) {
        vis = VB(n + 1, false);
        DFS(i, 1);
    }
    fout << diam;

}