Cod sursa(job #2619324)

Utilizator bem.andreiIceman bem.andrei Data 27 mai 2020 14:34:43
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;
ifstream r("radiatie.in");
ofstream w("radiatie.out");
int t[15002], c[15002], depth[15002];
struct ram{
    int a, b, cost;
}v[30002];
vector<int>g[15002];
bool cmp(ram a, ram b){
    return a.cost<b.cost;
}
int root(int a){
    if(t[a]==0){
        return a;
    }
    return root(t[a]);
}
void dep(int a){
    for(int i=0;i<g[a].size();i++){
        depth[g[a][i]]=depth[a]+1;
        dep(g[a][i]);
    }
}
int main()
{
    int n, m, q;
    r>>n>>m>>q;
    for(int i=0;i<m;i++){
        r>>v[i].a>>v[i].b>>v[i].cost;
    }
    sort(v, v+m, cmp);
    for(int i=0;i<m;i++){
        int a=root(v[i].a), b=root(v[i].b);
        if(a!=b){
            t[a]=b;
            g[b].push_back(a);
            c[a]=v[i].cost;
        }
    }
    int init=1;
    while(t[init]!=0){
        init++;
    }
    dep(init);
    while(q--){
        int a, b, rez=0;
        r>>a>>b;
        if(depth[a]<depth[b]){
            swap(a, b);
        }
        while(depth[a]>depth[b]){
            rez=max(rez, c[a]);
            a=t[a];
        }
        while(a!=b){
            rez=max(rez, max(c[a], c[b]));
            a=t[a];
            b=t[b];
        }
        w<<rez<<"\n";
    }
    return 0;
}