Cod sursa(job #3157729)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 16 octombrie 2023 19:11:31
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.19 kb
#ifndef LOCAL
    #pragma GCC optimize("Ofast")
#endif

#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;
using ci = const int;
using cll = const long long;

using namespace std;

/*******************************/
// INPUT / OUTPUT

#ifndef LOCAL
    ifstream in("radiatie.in");
    ofstream out("radiatie.out");
    #define cin in
    #define cout out
#endif
/*******************************/
/// GLOBAL DECLARATIONS

int N, M, Q;

struct Data
{
    int a, b, cost, idx;

    Data (int _a, int _b, int _cost, int _idx = -1)
    {
        a = _a; b = _b; cost = _cost; idx = _idx;
    }

    const bool operator < (const Data &other)const
    {
        if (cost == other.cost)
            return idx < other.idx;
        return cost < other.cost;
    }
};

struct DSU
{
    vector <int> tata, sz;

    DSU (int n)
    {
        tata.resize(n + 1);
        sz.resize(n + 1);

        reset(n);
    }

    inline void reset(int n)
    {
        for (int i = 1 ; i <= n ; ++ i)
        {
            tata[i] = i;
            sz[i] = 1;
        }
    }

    inline int get_tata(int node)
    {
        if (node == tata[node]) 
            return node;
        return (tata[node] = get_tata(tata[node]));
    }

    inline bool same_set(int a, int b)
    {
        return (get_tata(a) == get_tata(b));
    }

    void uneste(int a, int b)
    {
        a = get_tata(a);
        b = get_tata(b);

        if (sz[a] < sz[b])
            swap(a, b);
        
        tata[b] = a;
        sz[a] += sz[b];
    }
} dsu(10);

vector <int> rasp;
vector <Data> muchii, queries;
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    cin >> N >> M >> Q;

    int a, b, cost;
    for (int i = 0 ; i < M ; ++ i)
    {
        cin >> a >> b >> cost;
        
        if (a > b) swap(a, b);
        muchii.push_back({a, b, cost});
    }

    for (int i = 0 ; i < Q ; ++ i)
    {
        cin >> a >> b;

        queries.push_back({a, b, 0, i});
    }
}
///-------------------------------------
inline void solve_queries(int step)
{
    dsu.reset(N);
    sort(queries.begin(), queries.end());

    int i = 0; // index muchie curenta
    for (auto &q: queries)
    {
        while (i < M && q.cost + step >= muchii[i].cost)
        {
            dsu.uneste(muchii[i].a, muchii[i].b);
            ++ i;
        }

        if (!dsu.same_set(q.a, q.b))
            q.cost += step;
    }
}
///-------------------------------------
inline void Solution()
{
    dsu = DSU(N);
    sort(muchii.begin(), muchii.end());
    int step = (1 << 29);

    while (step)
    {
        solve_queries(step);
        step >>= 1;
    }

    rasp.resize(Q);
    for (auto q: queries)
        rasp[q.idx] = q.cost + 1;
}
///-------------------------------------
inline void Output()
{
    for (auto x: rasp)
        cout << x << "\n";
}
///-------------------------------------
int main()
{
    #ifndef LOCAL
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
    #endif
    ReadInput();
    Solution();
    Output();
    return 0;
}