Cod sursa(job #2409925)

Utilizator victorv88Veltan Victor victorv88 Data 19 aprilie 2019 15:57:41
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int>graph[15005];

struct d{
    int nod1, nod2, lungime;
}drumuri[30005];

bool operator<(d a, d b)
{
    return a.lungime<b.lungime;
}

int n, m, k, father[15005], height[15005], cost[15005], h=1;
int inaltime[15005];

int find_father(int ind)
{
    if (ind==father[ind])
    {
        return ind;
    }
    return find_father(father[ind]);
}

void unite(int ind1, int ind2, int indice_rel)
{
    if (height[ind1]>height[ind2])
    {
        father[ind2]=ind1;
        graph[ind1].push_back(ind2);
        cost[drumuri[indice_rel].nod2]=drumuri[indice_rel].lungime;
    }
    else
    {
        father[ind1]=ind2;
        graph[ind2].push_back(ind1);
        cost[drumuri[indice_rel].nod1]=drumuri[indice_rel].lungime;
    }
    if (height[ind1]==height[ind2])
    {
        height[ind2]++;
    }
}

void dfs(int ind)
{
    inaltime[ind]=h;
    for (auto &v:graph[ind])
    {
        h++;
        dfs(v);
        --h;
    }
}

void create_apm()
{
    sort(drumuri+1,drumuri+m+1);
    for (int i=1; i<=n; ++i)
        father[i]=i;
    for (int i=1; i<=m; ++i)
    {
        int parinte1=find_father(drumuri[i].nod1);
        int parinte2=find_father(drumuri[i].nod2);
        if (parinte1!=parinte2)
        {
            unite(parinte1,parinte2,i);
        }
    }
    for (int i=1; i<=n; ++i)
    {
        if (father[i]==i)
        {
            dfs(i);
            return;
        }
    }
}

int main() {
    int a, b, rez;
    f >> n >> m >> k;
    for (int i=1; i<=m; ++i)
    {
        f >> drumuri[i].nod1 >> drumuri[i].nod2 >> drumuri[i].lungime;
    }
    create_apm();
    for (int i=1; i<=k; ++i)
    {
        rez=0;
        f >> a >> b;
        if (inaltime[a]<inaltime[b])
            swap(a,b);
        while (inaltime[a]>inaltime[b])
        {
            rez=max(rez,cost[a]);
            a=father[a];
        }
        while (a!=b)
        {
            rez=max(rez,max(cost[a],cost[b]));
            a=father[a];
            b=father[b];
        }
        g << rez << '\n';
    }
    return 0;
}