Cod sursa(job #2400419)

Utilizator lupulescu2001Lupulescu Vlad lupulescu2001 Data 8 aprilie 2019 18:40:21
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

const int nMax = 15000;
vector<int> g[nMax + 5];
int tt[nMax + 5], lvl[nMax + 5], cost[nMax + 5];
int n, m, k;
struct muchie {
    int x, y, c;
}v[2 * nMax + 5];

bool comparator(muchie a, muchie b) {
    return a.c < b.c;
}

int Radacina(int x) {
    while(tt[x])
        x = tt[x];
    return x;
}

void DFS(int nod) {
    for (auto i : g[nod]) {
        lvl[i] = lvl[nod] + 1;
        DFS(i);
    }
}

int main() {
    fin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
        fin >> v[i].x >> v[i].y >> v[i].c;
    sort(v + 1, v + 1 + m, comparator);
    for (int i = 1; i <= m; i++) {
        int x = Radacina(v[i].x);
        int y = Radacina(v[i].y);
        if (x == y)
            continue;
        tt[x] = y;
        g[y].push_back(x);
        cost[x] = v[i].c;
    }
    for (int i = 1; i <= n; i++)
        if (tt[i] == 0)
            DFS(i);
    for (int i = 1; i <= k; i++) {
        int x, y;
        fin >> x >> y;
        if (lvl[x] < lvl[y])
            swap(x, y);
        int sol = 0;
        while (lvl[x] > lvl[y]) {
            sol = max(sol, cost[x]);
            x = tt[x];
        }
        while (x != y) {
            sol = max(sol, max(cost[x], cost[y]));
            x = tt[x];
            y = tt[y];
        }
        fout << sol << '\n';
    }
}