Cod sursa(job #1783009)

Utilizator NicusorTelescu Nicolae Nicusor Data 18 octombrie 2016 17:56:41
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.24 kb
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

struct XX
{
    int nod1,nod2,lung;
};

struct XY
{
    int loc,l;
};

struct cmp
{
    bool operator () (XX &a,XX &b)
    {
        return a.lung>b.lung;
    }
};

priority_queue <XX, vector <XX> , cmp> TY;
vector <XY> muchii[15001];

int t[15001],h[15001],nr_a,maximum;

struct
{
    int str,nr;
}koala[15001][15];

int find(int a)
{
    int x=a,y;
    while (x!=t[x]) x=t[x];
    while (a!=t[a])
    {
        y=t[a];
        t[a]=x;
        a=y;
    }
    return x;
}

void radacina(int x,int rand)
{
    h[x]=rand;
    int nrx=muchii[x].size();
    for (int i=0;i<nrx;i++)
    {
        if (t[muchii[x][i].loc]!=0)
        {
            t[muchii[x][i].loc]=0;
            koala[muchii[x][i].loc][0].str=x;
            koala[muchii[x][i].loc][0].nr=muchii[x][i].l;
            radacina(muchii[x][i].loc,rand+1);
        }
    }
}

int egal(int a,int b,int max1)
{
    if (h[a]==h[b])
    {
        nr_a=a;
        return max1;
    }
    else
    {
        int i=1;
        for (;h[koala[a][i].str]>h[b];i++);
        i--;
        if (koala[a][i].nr > max1)
            max1=koala[a][i].nr;
        return egal(koala[a][i].str,b,max1);

    }
}

void lca(int a,int b)
{
    if (koala[a][0].str==koala[b][0].str)
    {
        if (maximum < koala[a][0].nr) maximum=koala[a][0].nr;
        if (maximum < koala[b][0].nr) maximum=koala[b][0].nr;
    }
    else
    {
        int i=1;
        for (;koala[a][i].str != koala[b][i].str && koala[b][i].str!=0;i++);
        i--;
        if (maximum < koala[a][i].nr) maximum=koala[a][i].nr;
        if (maximum < koala[b][i].nr) maximum=koala[b][i].nr;
        lca(koala[a][i].str,koala[b][i].str);
    }
}

int main()
{
    freopen("radiatie.in","r",stdin);
    freopen("radiatie.out","w",stdout);
    int n,m,k,i;
    scanf("%d %d %d\n",&n,&m,&k);
    for (i=1;i<=m;i++)
    {
        int a,b,c;
        scanf("%d %d %d\n",&a,&b,&c);
        TY.push({a,b,c});

        if (i<=n)
        t[i]=i;
    }
    for (;i<=n;i++) t[i]=i;

    int cnt=1;
    while (!TY.empty() && cnt!=n)
    {
        XX yuk=TY.top();
        TY.pop();

        int f_a=find(yuk.nod1);
        int f_b=find(yuk.nod2);
        if (f_a!=f_b)
        {
            cnt++;
            t[f_a]=f_b;
            muchii[yuk.nod1].push_back({yuk.nod2,yuk.lung});
            muchii[yuk.nod2].push_back({yuk.nod1,yuk.lung});
        }
    }

    t[1]=0;
    radacina(1,1);

    for (int i=1;i<=n;i++)
    {
        for (int j=1;(1<<j)+i-1<=n;j++)
        {
            koala[i][j].str=koala[koala[i][j-1].str][j-1].str;
            if (koala[i][j-1].nr > koala[koala[i][j-1].str][j-1].nr)
                koala[i][j].nr=koala[i][j-1].nr;
            else koala[i][j].nr=koala[koala[i][j-1].str][j-1].nr;
        }
    }

    for (int i=1;i<=k;i++)
    {
        int a,b,aux;
        scanf("%d %d\n",&a,&b);

        if (h[b]>h[a]) aux=b, b=a, a=aux;

        if (h[a]!=h[b])
            maximum=egal(a,b,0);

        a=nr_a;
        if (a==b) printf("%d\n",maximum);
        else
        {
            lca(a,b);
            printf("%d\n",maximum);
        }
    }
}