Cod sursa(job #3195382)

Utilizator Robert_NicuNicu Robert Cristian Robert_Nicu Data 20 ianuarie 2024 17:40:20
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <bits/stdc++.h>
#define DIM 100001
using namespace std;

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

int n, m, q, a, b, c, ans;
int i, j;
int tata[DIM], parcurs[DIM], depth[DIM], dp[DIM];
struct elem{
    int x, y, cost;
};
vector <elem> G;
vector <pair <int, int> > G1[DIM];

bool comp(elem a, elem b){
    return a.cost<b.cost;
}

int get_root(int nod){
    while(tata[nod]>0)
        nod=tata[nod];
    return nod;
}

void join(int nod1, int nod2){
    int root1=get_root(nod1);
    int root2=get_root(nod2);
    if(root1!=root2){
        if(tata[root1]<tata[root2]){
            tata[root1]+=tata[root2];
            tata[root2]=root1;
        }else{
            tata[root2]+=tata[root1];
            tata[root1]=root2;
        }
    }
}

void dfs(int nod, int val){
    parcurs[nod]=1;

    depth[nod]=val;
    for(auto fiu:G1[nod]){
        if(!parcurs[fiu.first]){
            dp[fiu.first]=fiu.second;
            dfs(fiu.first, val+1);
        }
    }
}

int main(){
    fin>>n>>m>>q;
    for(i=1; i<=m; i++){
        fin>>a>>b>>c;
        G.push_back({a, b, c});
    }
    for(i=1; i<=n; i++)
        tata[i]=-1;
    sort(G.begin(), G.end(), comp);
    for(auto muchie:G){
        if(get_root(muchie.x)!=get_root(muchie.y)){
            join(muchie.x, muchie.y);
            G1[muchie.x].push_back(make_pair(muchie.y, muchie.cost));
            G1[muchie.y].push_back(make_pair(muchie.x, muchie.cost));
        }
    }
    dfs(get_root(1), 0);
    while(q--){
        fin>>a>>b;
        ans=0;
        while(depth[a]>depth[b]){
            ans=max(ans, dp[a]);
            a=tata[a];
        }
        while(depth[a]<depth[b]){
            ans=max(ans, dp[b]);
            b=tata[b];
        }
        while(a!=b){
            ans=max(max(dp[a], dp[b]), ans),
            a=tata[a];
            b=tata[b];
        }
        fout<<ans<<"\n";
    }
}