Cod sursa(job #3183296)

Utilizator Cazacu2006RazvanRazvan Cazacu Cazacu2006Razvan Data 11 decembrie 2023 14:08:57
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int n,m,k,tata[15001],cost[15001],radacina,nivel[20001];
struct numar
{
    int x,y,cost;
}v[40001];
vector <int> A[20001];
int cmp(numar a,numar b)
{
    return a.cost<b.cost;
}
int root(int x)
{
    while(tata[x]>0)
    {
        x=tata[x];
    }
    return x;
}
void dfs(int nod,int lvl)
{
    nivel[nod]=lvl;
    for(auto i : A[nod])
    {
        dfs(i,lvl+1);
    }
}
int main()
{
    fin>>n>>m>>k;
    for(int i=1;i<=m;i++)
    {
        fin>>v[i].x>>v[i].y>>v[i].cost;
    }
    sort(v+1,v+m+1,cmp);
    for(int i=1;i<=n;i++)
        tata[i]=-1;
    int p=0;
    for(int i=1;p<n-1;i++)
    {
        int rx=root(v[i].x);
        int ry=root(v[i].y);
        if(rx!=ry)
        {
            p++;
            if(tata[rx]<tata[ry])
            {
                A[rx].push_back(ry);
                cost[ry]=v[i].cost;
                tata[rx]+=tata[ry];
                tata[ry]=rx;
                radacina=rx;
            }
            else
            {
                A[ry].push_back(rx);
                cost[rx]=v[i].cost;
                tata[ry]+=tata[rx];
                tata[rx]=ry;
                radacina=ry;

            }
        }
    }
    dfs(radacina,1);
    for(int i=1;i<=k;i++)
    {
        int x,y;
        fin>>x>>y;
        int max0=-10000000;
        while(nivel[x]>nivel[y])
        {
            max0=max(max0,cost[x]);
            x=tata[x];
        }
        while(nivel[y]>nivel[x])
        {
            max0=max(max0,cost[y]);
            y=tata[y];
        }
        while(y!=x)
        {
            max0=max(max0,max(cost[x],cost[y]));
            x=tata[x];
            y=tata[y];
        }

        fout<<max0<<"\n";
    }
    return 0;
}