Cod sursa(job #1489902)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 22 septembrie 2015 11:29:16
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>
#include <algorithm>
using namespace std;

ifstream f("radiatie.in");
ofstream g("radiatie.out");

long n,m,k,nrm,i,j,x,y,rt;
struct arb{
    long ant,niv,lg;
}h[15005];
struct edge{
    long x,y,c;
}ed[30004];

long comp(edge a,edge b)
{
    return a.c<b.c;
}

long root(long nod,long &lg)
{
    lg=0;
    while (h[nod].ant!=nod)
    {
        nod=h[nod].ant;
        lg++;
    }
    return nod;
}

void apm()
{
    long rx,ry,lgx=1,lgy=1;
    nrm=0;
    int i=0;
    while (nrm!=n-1)
    {
        i++;
        rx=root(ed[i].x,lgx);
        ry=root(ed[i].y,lgy);
        if (rx!=ry)
        {
            if (lgx<=lgy)
            {
                h[rx].ant=ry;
                h[rx].lg=ed[i].c;
            }
            else
            {
                h[ry].ant=rx;
                h[ry].lg=ed[i].c;
            }
            nrm++;
        }
    }
}

long find(long nod)
{
    if (h[nod].niv!=0)
        return h[nod].niv+1;
    else
        return (h[nod].niv=find(h[nod].ant))+1;
}

long maxEdge(long nod1,long nod2)
{
    long mx=0;
    while (nod1!=nod2)
    {
        if (h[nod1].niv<=h[nod2].niv)
        {
            if (h[nod2].lg>mx)
                mx=h[nod2].lg;
            nod2=h[nod2].ant;
        }
        else
        {
            if (h[nod1].lg>mx)
                mx=h[nod1].lg;
            nod1=h[nod1].ant;
        }
    }
    return mx;
}

int main()
{
    f>>n>>m>>k;
    for (i=1;i<=n;i++)
        f>>ed[i].x>>ed[i].y>>ed[i].c;
    for (i=1;i<=n;i++)
        h[i].ant=i;
    sort(ed+1,ed+m+1,comp);
    apm();
    rt=root(1,x);
    h[rt].niv=1;
    for (i=1;i<=n;i++)
        if (h[i].niv==0)
            find(i);

    for (i=1;i<=k;i++)
    {
        f>>x>>y;
        g<<maxEdge(x,y)<<'\n';
    }


    return 0;
}