Cod sursa(job #2998679)

Utilizator tomaionutIDorando tomaionut Data 9 martie 2023 20:27:52
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("radiatie.in");
ofstream fout("radiatie.out");

int n, m, k, l[15005], sol[15005];
struct vrajeala
{
    int x, y, cost;
    bool operator < (const vrajeala A) const
    {
        return cost < A.cost;
    }
}a[30005];

unordered_map <int, bool> v[15005];

struct DSU
{
    vector <int> t;

    DSU(int n)
    {
        t.resize(n);
    }

    int Find(int x)
    {
        int r = x, y;
        while (t[r])
            r = t[r];

        while (r != x)
        {
            y = t[x];
            t[x] = r;
            x = y;
        }
        return r;
    }

    void Union(int x, int y, int c)
    {
        if (l[y] > l[x])
            swap(x, y);
        l[x] += l[y];
        for (auto w : v[y])
        {
            if (v[x][w.first])
                sol[w.first] = c;
            v[x][w.first] = 1;
        }
        t[y] = x;
    }

};

int32_t main()
{
    int i, x, y;
    fin >> n >> m >> k;
    for (i = 1; i <= m; i++)
        fin >> a[i].x >> a[i].y >> a[i].cost;
    DSU tree(n + 5);
    sort(a + 1, a + m + 1);
    for (i = 1; i <= n; i++)
        l[i] = 1;

    for (i = 1; i <= k; i++)
    {
        fin >> x >> y;
        v[y][i] = 1;
        v[x][i] = 1;
    }
    for (i = 1; i <= m; i++)
    {
        x = tree.Find(a[i].x);
        y = tree.Find(a[i].y);
        if (x != y)
            tree.Union(x, y, a[i].cost);
    }

    for (i = 1; i <= k; i++)
        fout << sol[i] << "\n";

    return 0;
}