Cod sursa(job #2451035)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 25 august 2019 14:07:00
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda repost Marime 1.94 kb
#include <fstream>
#include <algorithm>
#include <vector>
#define NMAX 15002
#define MMAX 30002
#define cost first
#define a second.first
#define b second.second
#define triple pair <int, pair <int,int> >
using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");

int n, m, k, t[NMAX], h[NMAX], tQ[NMAX], pz1[NMAX],pz2[NMAX],cst,ans[NMAX];
triple v[MMAX];
vector < int > Q[NMAX];

int where(int nod)
{
    if(t[nod] == nod)
        return nod;
    return t[nod] = where(t[nod]);
}
int whereQ(int nod)
{
    if(tQ[nod] == nod)
        return nod;
    return tQ[nod] = whereQ(tQ[nod]);
}
void inters(int x, int y)
{
    if(h[x] >= h[y])
        t[y] = x, h[x] += h[y];
    else
        t[x] = y, h[y] += h[x];

    int radX = whereQ(tQ[x]);
    int radY = whereQ(tQ[y]);

    if( Q[radX].size() < Q[radY].size())
        swap(radX,radY);

    for(int i = 0 ; i < Q[radY].size(); ++i)
    {
        int val = Q[radY][i];
        if(whereQ(pz1[val]) == radX && whereQ(pz2[val] == radY))
            ans[val] = cst;
        else if
           (whereQ(pz1[val]) == radY && whereQ(pz2[val] == radX))
            ans[val] = cst;
        else
            Q[radX].push_back(val);
    }

    tQ[radY] = radX;
}
int main()
{
    f >> n >> m >> k;
    for(int i = 1; i <= m; ++i)
        f >> v[i].a >> v[i].b >> v[i].cost;

    sort(v + 1, v + m + 1);
    for(int i = 1; i <= k; ++i)
    {
        int x,y;
        f >> x >> y;
        pz1[i] = x;
        pz2[i] = y;
        Q[x].push_back(i);
        Q[y].push_back(i);

    }
    for(int i = 1; i <= n; ++i)
    { t[i] = i; tQ[i] = i; h[i] = 1;}
    for(int i = 1; i <= m; ++i)
    {
        cst = v[i].cost;
        if(where(v[i].a) != where(v[i].b))
        {
            inters(where(v[i].a), where(v[i].b) );
        }
    }
    for(int i = 1; i <= k; ++i)
        g << ans[i] << '\n';
    f.close();
    g.close();
    return 0;
}