Cod sursa(job #2624823)

Utilizator CraniXortDumitrescul Eduard CraniXort Data 5 iunie 2020 14:52:27
Problema Interclasari Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.83 kb
#include <bits/stdc++.h>
#define maxn 16005

std::ifstream fin ("razboi.in");
std::ofstream fout ("razboi.out");

void dfsDown (int node, std::vector <std::pair <int, int>> *edge, int *distDown, bool *visited){
    visited[node] = true;
    for (int i=0; i<edge[node].size(); i++){
        int next = edge[node][i].first;
        int cost = edge[node][i].second;
        if (visited[next] == false){
            dfsDown (next, edge, distDown, visited);
            distDown[node] = std::max (distDown[node], distDown[next] + cost);
        }
    }
}

void dfsUp (int node, std::vector <std::pair <int, int>> *edge, int *distUp, int *distDown, bool *visited){
    visited[node] = false;
    std::priority_queue <int> pq;
    pq.push (0);
    for (int i=0; i<edge[node].size(); i++){
        int next = edge[node][i].first;
        int cost = edge[node][i].second;
        if (visited[next] == true)
            pq.push (distDown[next] + cost);
    }

    for (int i=0; i<edge[node].size(); i++){
        int next = edge[node][i].first;
        int cost = edge[node][i].second;
        if (visited[next] == true){
            distUp[next] = distUp[node] + cost;
            if (pq.top() == cost + distDown[next]){
                pq.pop();
                distUp[next] = std::max (distUp[next], pq.top()+cost);
                pq.push (cost + distDown[next]);
            }
            else
                distUp[next] = std::max (distUp[next], pq.top()+cost);
        }
    }

     for (int i=0; i<edge[node].size(); i++){
        int next = edge[node][i].first;
        int cost = edge[node][i].second;
        if (visited[next] == true)
            dfsUp (next, edge, distUp, distDown, visited);
    }

}


int main()
{
    int T, t;
    fin >> T;
    for (t=1; t<=T; t++){
        fout << "Testul nr #" << t << '\n';
        int V, E, src, dest, i, cost;
        fin >> V; E = V - 1;

        std::vector <std::pair <int, int>> edge[V+1];
        int distDown[V+1] = {}, distUp[V+1] = {};
        bool visited[V+1] = {};

        for (i=0; i<E; i++){
            fin >> src >> dest >> cost;
            edge[src].push_back ({dest, cost});
            edge[dest].push_back ({src, cost});
        }

        dfsDown (1, edge, distDown, visited);
        distUp[1] = 0;
        dfsUp (1, edge, distUp, distDown, visited);

        /*
        for (i=1; i<=V; i++)
            fout << distDown[i] << ' ';
        fout << '\n';
        for (i=1; i<=V; i++)
            fout << distUp[i] << ' ';
        fout << '\n';
        */

        int min = 1e9;
        for (i=1; i<=V; i++)
            min = std::min (min, std::max (distUp[i], distDown[i]));

        for (i=1; i<=V; i++){
            if (min == std::max (distUp[i], distDown[i]))
                fout << i << '\n';
        }
    }
    return 0;
}