Cod sursa(job #2646415)

Utilizator rares404AlShaytan - Balasescu Rares rares404 Data 1 septembrie 2020 09:59:21
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.59 kb
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream f("radiatie.in");
ofstream g("radiatie.out");
 
int n,m,k,i,t[15010],depth[15010],st[30][15010],gtmax[30][15010];
pair<int,pair<int,int> > PP[30010];
vector<pair<int,int> > v[15010];
 
int getpr(int poz)
{
    int aux=poz;
    for(;aux!=t[aux];aux=t[aux]);
    for(;t[poz]!=aux;)
    {
        int auxx=t[poz];
        t[poz]=auxx;
        poz=auxx;
    }
    return aux;
}
void dfs(int poz)
{
    for(int i=1,siz=2;siz<=depth[poz];i++,siz<<=1)
    {
        st[i][poz]=st[i-1][st[i-1][poz]];
        gtmax[i][poz]=max(gtmax[i-1][poz],gtmax[i-1][st[i-1][poz]]);
    }
    for(auto it:v[poz])
        if(it.first!=st[0][poz])
        {
            st[0][it.first]=poz;
            gtmax[0][it.first]=it.second;
            depth[it.first]=depth[poz]+1;
            dfs(it.first);
        }
}
pair<int,int> movup(int nod,int dst)
{
    pair<int,int> ret={nod,0};
    for(int i=0;i<=25&&dst;i++,dst>>=1)
        if(dst&1)
        {
            ret.second=max(ret.second,gtmax[i][ret.first]);
            ret.first=st[i][ret.first];
        }
    return ret;
}
 
int main()
{
    f>>n>>m>>k;
    for(i=1;i<=n;i++)t[i]=i;
    for(i=1;i<=m;i++)
        f>>PP[i].second.first>>PP[i].second.second>>PP[i].first;
    sort(PP+1,PP+m+1);
    for(i=1;i<=m;i++)
    {
        int a,b,c;
        a=PP[i].second.first,b=PP[i].second.second;
        c=PP[i].first;
        int A=getpr(a);
        int B=getpr(b);
        if(A!=B)
        {
            t[A]=B;
            v[a].push_back({b,c});
            v[b].push_back({a,c});
        }
    }
    dfs(1);
//    for(i=1;i<=n;i++)
//    {
//        g<<st[0][i]<<' '<<st[1][i]<<' '<<st[2][i]<<' '<<st[3][i]<<' ' <<st[4][i]<<' '<<st[5][i]<<" | ";
//        g<<gtmax[0][i]<<' '<<gtmax[1][i]<<' '<<gtmax[2][i]<<' '<<gtmax[3][i]<<' '<<gtmax[4][i]<<' '<<gtmax[5][i]<<'\n';
//    }
//    for(i=1;i<=n;i++)
//    {
//        g<<i<<": ";
//        g<<" ("<<depth[i]<<") ";
//        for(auto it:v[i])
//            g<<it.first<<' '<<it.second<<" | ";
//        g<<'\n';
//    }
    for(;k;k--)
    {
        int a,b;
        f>>a>>b;
        if(depth[a]<depth[b])
            swap(a,b);
        pair<int,int> x=movup(a,depth[a]-depth[b]);
        int ans=x.second;a=x.first;
        if(a==b)
        {
            g<<ans<<'\n';
            continue;
        }
        for(i=25;i>=0;i--)
            if(st[i][a]&&st[i][b]&&st[i][a]!=st[i][b])
            {
                ans=max(ans,max(gtmax[i][a],gtmax[i][b]));
                a=st[i][a];
                b=st[i][b];
            }
        g<<max(ans,max(gtmax[0][a],gtmax[0][b]))<<'\n';
    }
    return 0;
}