Cod sursa(job #3152942)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 27 septembrie 2023 10:20:02
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
    #pragma GCC optimize("Ofast")
#endif // LOCAL



#ifndef LOCAL
    string name="radiatie";
    ifstream in(name+".in");
    ofstream out(name+".out");
    #define cin in
    #define cout out
#endif // LOCAL


const int MN = 15005;

using pii =pair<int,int>;
using vvi = vector<vector<int>>;
using vvp = vector<vector<pii>>;
using vi = vector<int>;

struct edge{
    int a, b,c;
} ed[MN*2];

struct cmp{
    bool operator () (edge a, edge b){
        return a.c<b.c;
    }  
    
};

int n,m,q;

int h[MN], par[MN], pcost[MN];

int getPar(int a)
{
    if(par[a]==a) return a;
    return getPar(par[a]);
}

void join(edge x)
{
    int a=x.a, b=x.b, c=x.c;
    a=getPar(a);
    b=getPar(b);
    if(a==b) return;
    if(h[a]<h[b]) swap(a,b);
    if(h[a]==h[b]) h[a]++;
    
    pcost[b]=c;
    par[b]=a;
}


int main()
{
    cin>>n>>m>>q;
    for(int i=1;i<=m;i++)
    {
        cin>>ed[i].a>>ed[i].b>>ed[i].c;
    }
    sort(ed+1,ed+m+1,cmp());
    
    for(int i=1;i<=n;i++)
    {
        par[i]=i;
        h[i]=1;
    }
    
    for(int i=1;i<=m;i++)
    {
        join(ed[i]);
    }
    
    int a,b;
    for(int i=1;i<=q;i++)
    {
        cin>>a>>b;
        int mx=0;
        //cerr<<a<<' '<<b<<' '<<h[a]<<' '<<h[b]<<'\n';
        while(a!=b)
        {
            if(h[a]>h[b]) swap(a,b);
            mx=max(mx,pcost[a]);
            a=par[a];
        }
        cout<<mx<<'\n';
    }
    return 0;
}