Cod sursa(job #3126763)

Utilizator FlaviusMMazilu Flavius Romeo FlaviusM Data 6 mai 2023 22:50:27
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.94 kb
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#define MAX_VAL (1 << 30)
class Task {
public:
    void solve() {
        read_input();
        print_output(get_result());
    }

private:
    // numarul maxim de noduri
    static constexpr int NMAX = (int)1e5 + 5; // 10^5 + 5 = 100.005

    // n = numar de noduri, m = numar de muchii/arce
    int n, m;

    // adj[node] = lista de adiacenta a nodului node
    // exemplu: daca adj[node] = {..., neigh, ...} => exista muchia (node, neigh)
    vector<int> adj[NMAX];

    // nodul sursa in parcurgerea BFS
    int source;

    void read_input() {
        ifstream fin("darb.in");
        fin >> n;
        for (int i = 1, x, y; i <= n; i++) {
            fin >> x >> y; // muchie (x, y)
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        fin.close();
    }

    int get_result() {
        vector<int> d(n + 1, MAX_VAL);
        source = 1;
        d[source] = 0;
        vector<int> parents(n + 1, -1);
        parents[source] = 0;

        queue<int> q;
        q.push(source);
        while (!q.empty()) {
            auto node = q.front();
            q.pop();
            for (auto neigh : adj[node]) {
                if (parents[neigh] == -1) {
                    q.push(neigh);
                    parents[neigh] = node;
                    d[neigh] = d[node] + 1;
                }
            }
        }

        int local_max = -1;
        int new_source = -1;
        for (int i = 1; i <= n; i++) {
            if (d[i] != MAX_VAL && d[i] > local_max) {
                local_max = d[i];
                new_source = i;
            }
        }

        vector<int> dd(n + 1, MAX_VAL);
        vector<int> parentss(n + 1, -1);

        parentss[new_source] = 0;
        dd[new_source] = 0;
        q.push(new_source);

        while (!q.empty()) {
            auto node = q.front();
            q.pop();
            for (auto neigh : adj[node]) {
                if (parentss[neigh] == -1) {
                    q.push(neigh);
                    parentss[neigh] = node;
                    dd[neigh] = dd[node] + 1;
                }
            }
        }
        for (unsigned long i = 0; i < dd.size(); i++) {
            if (dd[i] == MAX_VAL)
                dd[i] = -1;
        }
        return *max_element(dd.begin(), dd.end()) + 1;
    }

    void print_output(int dist) {
        ofstream fout("darb.out");
        fout << dist << endl;
        fout.close();
    }
};

// [ATENTIE] NU modifica functia main!
int main() {
    // * se aloca un obiect Task pe heap
    // (se presupune ca e prea mare pentru a fi alocat pe stiva)
    // * se apeleaza metoda solve()
    // (citire, rezolvare, printare)
    // * se distruge obiectul si se elibereaza memoria
    auto* task = new (nothrow) Task(); // hint: cppreference/nothrow
    if (!task) {
        cerr << "new failed: WTF are you doing? Throw your PC!\n";
        return -1;
    }
    task->solve();
    delete task;
    return 0;
}