Cod sursa(job #2737619)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 4 aprilie 2021 22:01:56
Problema Radiatie Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <bits/stdc++.h>

using namespace std;

#ifndef LOCAL

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

#define cin in
#define cout out

#endif //LOCAL

const int NMAX = 16 * 1024;
const int QMAX = 32 * 1024;

int tata[NMAX];
int dpth[NMAX];

int getTata(int x)
{
    if(x == tata[x]) return x;
    tata[x] = getTata(tata[x]);
    return tata[x];
}

void join(int x, int y)
{
    x = getTata(x);
    y = getTata(y);

    if(x == y) return;

    if(dpth[x] > dpth[y]) swap(x, y);
    tata[x] = y;
    dpth[y] += (dpth[x] == dpth[y]);

    return;
}

vector<pair<int, pair<int, int>>> edges;
int ans[QMAX];
vector<pair<int, pair<int, int>>> queries;

int main()
{
    int n, m, q; cin >> n >> m >> q;

    for(int i = 1; i <= n; i++)
    {
        tata[i] = i;
        dpth[i] = 1;
    }

    edges.push_back(make_pair(0, make_pair(0, 0)));
    for(int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;

        edges.push_back(make_pair(c, make_pair(a, b)));
    }

    for(int i = 0; i < q; i++)
    {
        int x, y; cin >> x >> y;
        queries.push_back(make_pair(i, make_pair(x, y)));
    }

    sort(edges.begin(), edges.end());

    for(auto e : edges)
    {
        join(e.second.first, e.second.second);

        vector<pair<int, pair<int,int>>> nqueries;
        for(auto qr : queries)
        {
            if(getTata(qr.second.first) == getTata(qr.second.second))
                ans[qr.first] = e.first;
            else
                nqueries.push_back(qr);
        }

        queries = nqueries;
    }

    for(int i = 0; i < q; i++)
        cout << ans[i] << '\n';
}