Cod sursa(job #3194218)

Utilizator AlexSerban21Serban Alexandru AlexSerban21 Data 17 ianuarie 2024 12:30:48
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin ("radiatie.in");
ofstream fout ("radiatie.out");
struct muchie
{
    int x,y,c;
};
muchie v[30001];
int x,y,xx,yy,nivx,nivy,n,m,q,sol,i,dp[15001],t[15001],nrc[15001];
int cmp (const muchie &a,const muchie &b)
{
    return a.c<b.c;
}
int f (int nod)
{
    while (t[nod]!=0)
        nod=t[nod];
    return nod;
}
void reun (int a,int b)
{
    x=f (a);
    y=f (b);
    if (nrc[x]<nrc[y])
    {
        t[x]=y;
        dp[x]=v[i].c;
        nrc[y]+=nrc[x];
    }
    else
    {
        t[y]=x;
        dp[y]=v[i].c;
        nrc[x]+=nrc[y];
    }
}
int main()
{
    fin>>n>>m>>q;
    for (i=1; i<=m; i++)
        fin>>v[i].x>>v[i].y>>v[i].c;
    sort (v+1,v+m+1,cmp);
    for (i=1; i<=m; i++)
    {
        if (f (v[i].x)!=f (v[i].y))
            reun (v[i].x,v[i].y);
    }
    for (i=1; i<=q; i++)
    {
        fin>>x>>y;
        xx=x;
        while (xx!=0)
        {
            nivx++;
            xx=t[xx];
        }
        yy=y;
        while (yy!=0)
        {
            nivy++;
            yy=t[yy];
        }
        if (nivx<nivy)
        {
            swap (x,y);
            swap (nivx,nivy);
        }
        sol=0;
        while (nivx>nivy)
        {
            sol=max (sol,dp[x]);
            x=t[x];
            nivx--;
        }
        while (x!=y)
        {
            sol=max (sol,max (dp[x],dp[y]));
            x=t[x];
            y=t[y];
        }
        fout<<sol<<"\n";
    }
    return 0;
}