Cod sursa(job #2715928)

Utilizator mjmilan11Mujdar Milan mjmilan11 Data 4 martie 2021 13:23:04
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
const int NMAX = 30005;
struct idk{
    int x,y,cost;
};
idk a[NMAX];
int cst[NMAX],dad[NMAX],dim[NMAX];
int n,m,k,x,y,rasp;
bool cmp(idk a,idk b){
    return a.cost < b.cost;
}
int get_root(int node){
    while(node!=dad[node])
        node=dad[node];
    return node;
}
void solve(int node1,int node2,int i){
    node1=get_root(node1);
    node2=get_root(node2);
    if(node1!=node2){
        if(dim[node1]==dim[node2]) dim[node2]++;
        if(dim[node1]<dim[node2]){
            dad[node1]=node2;
            cst[node1]=a[i].cost;
        } else {
            dad[node2]=node1;
            cst[node2]=a[i].cost;
        }
    }
}
int main()
{
    fin >> n >> m >> k;
    for(int i=1;i<=n;i++) dad[i]=i;
    for(int i=1;i<=m;i++) fin >> a[i].x >> a[i].y >> a[i].cost;
    sort(a+1,a+m+1,cmp);
    for(int i=1;i<=m;i++) solve(a[i].x,a[i].y,i);
    for(int i=1;i<=k;i++){
        rasp=0;
        fin >> x >> y;
        while(x!=y){
            if(dim[x]<dim[y]){
                rasp=max(rasp,cst[x]);
                x=dad[x];
            } else {
                rasp=max(rasp,cst[y]);
                y=dad[y];
            }
        }
        fout << rasp << '\n';
    }
    return 0;
}