Cod sursa(job #3283276)

Utilizator Frant_IoanaFrant Ioana Frant_Ioana Data 8 martie 2025 19:48:35
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda oji_go_11_12_2 Marime 2.05 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;

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

struct muchie{
    int x, y, c;
} muchii[30001];

bool fcmp(muchie a, muchie b){
    if(a.c > b.c)
        return a.c < b.c;
    return true;
}

int n, m, k, tata[15001], lg[15001], costuri[15001];
vector<pair<int, int>> v[15001];

int rad(int x){
    while(x != tata[x])
        x = tata[x];
    return x;
}

void unify(int rx, int ry, int cost){
    if(lg[rx] < lg[ry]){
        tata[rx] = ry;
        costuri[rx] = cost;
    }
    else if(lg[rx] > lg[ry]){
        tata[ry] = rx;
        costuri[ry] = cost;
    }
    else{
        tata[ry] = rx;
        costuri[ry] = cost;
        lg[rx]++;
    }
}

int main(){
    fin >> n >> m >> k;

    for(int i = 1; i <= n; i++){
        tata[i] = i;
        lg[i] = 1;
    }

    for(int i = 1; i <= m; i++){
        fin >> muchii[i].x >> muchii[i].y >> muchii[i].c;
        v[muchii[i].x].push_back({muchii[i].y, muchii[i].c});
        v[muchii[i].y].push_back({muchii[i].x, muchii[i].c});
    }
    
    sort(muchii + 1, muchii + m + 1, fcmp);

    for(int i = 1; i <= m; i++){
        int x = muchii[i].x;
        int y = muchii[i].y;
        int cost = muchii[i].c;

        int rx = rad(x), ry = rad(y);
        if(rx == ry) continue;

        unify(rx, ry, cost);
    }

    for(int i = 1; i <= k; i++){
        int x, y;
        fin >> x >> y;

        int levelx = 0, levely = 0;
        int cpx = x, cpy = y;
        while(cpx != tata[x])
            cpx = tata[x], levelx++;
        while(cpy != tata[y])
            cpy = tata[y], levely++;
        
        if(levelx > levely){
            swap(x, y);
            swap(levelx, levely);
        }

        int maxi = costuri[y];
        while(levely > levelx){
            y = tata[y];
            maxi = max(maxi, costuri[y]);
            levely--;
        }

        while(x != y){
            maxi = max(maxi, costuri[x]);
            maxi = max(maxi, costuri[y]);
            x = tata[x];
            y = tata[y];
        }

        fout << maxi << '\n';
    }
}