Cod sursa(job #3308457)

Utilizator Nasa1004Ema Nicole Gheorghe Nasa1004 Data 25 august 2025 11:55:02
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.97 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;
const int NMAX = 15000;
const int LOG = 15;

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

struct muchii{
    int a, b, cost;
    bool operator <(const muchii & rhs) const {
        return cost < rhs.cost;
    }
}nr[2 * NMAX + 2];

struct DSU{
    vector <int> parent, sizes;
    void init(int n) {
        parent.resize(n + 1);
        sizes.assign(n + 1, 1);
        for(int i = 1; i <= n; i++)
            parent[i] = i;
    }
    int findd(int u) {
        if(u == parent[u])
            return u;
        return parent[u] = findd(parent[u]);
    }
    bool unite(int u, int w) {
        u = findd(u);
        w = findd(w);
        if(u == w)
            return false;
        if(sizes[w] > sizes[u])
            swap(u, w);
        parent[w] = u;
        sizes[u] += sizes[w];
        sizes[w] = 0;
        return true;
    }
}dsu;

struct edges{
    int nod, cost;
};
vector <vector <edges>> v;
int depth[NMAX + 2];
int up[NMAX + 2][LOG + 1]; ///pt lca
int upmax[NMAX + 2][LOG + 1]; ///muchia max pe saritura de 2^j

bool viz[NMAX + 2];
void dfs(int start, int tata) {
    viz[start] = 1;
    for(auto x : v[start]) {
        if(x.nod == tata)
            continue;
        depth[x.nod] = depth[start] + 1;
        ///build lca
        up[x.nod][0] = start;
        upmax[x.nod][0] = x.cost;
        for(int j = 1; j < LOG; j++) {
            up[x.nod][j] = up[up[x.nod][j - 1]][j - 1];
            upmax[x.nod][j] = max(upmax[x.nod][j - 1], upmax[up[x.nod][j - 1]][j - 1]);
        }
        dfs(x.nod, start);
    }
}

int querylca(int a, int b) {
    if(depth[a] < depth[b])
        swap(a, b);
    int maxx = 0;
    int k = depth[a] - depth[b];
    for(int j = LOG - 1; j >= 0; j--) {
        if(k & (1 << j)) {
            maxx = max(maxx, upmax[a][j]);
            a = up[a][j];
        }
    }
    if(a == b)
        return maxx;
    for(int j = LOG - 1; j >= 0; j--) {
        if(up[a][j] != up[b][j]) {
            maxx = max(maxx, max(upmax[a][j], upmax[b][j]));
            a = up[a][j];
            b = up[b][j];
        }
    }
    maxx = max(maxx, max(upmax[a][0], upmax[b][0]));
    return maxx;
}

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    for(int i = 1; i <= m; i++)
        cin >> nr[i].a >> nr[i].b >> nr[i].cost;
    sort(nr + 1, nr + m + 1);
    dsu.init(n);
    v.resize(n + 1);
    for(int i = 1; i <= m; i++) {
        if(dsu.unite(nr[i].a, nr[i].b)) {
            //cout << nr[i].a << " " << nr[i].b << " " << nr[i].cost << '\n';
            v[nr[i].a].push_back({nr[i].b, nr[i].cost});
            v[nr[i].b].push_back({nr[i].a, nr[i].cost});
        }
    }
    for(int i = 1; i <= n; i++) {
        if(!viz[i])
            dfs(i, 0);
    }
    while(q--) {
        int a, b;
        cin >> a >> b;
        cout << querylca(a, b) << '\n';
    }
    /*for(int i = 1; i <= n; i++) {
        for(int j = 0; j <= 4; j++)
            cout << upmax[i][j] << " ";
        cout << '\n';
    }*/
    return 0;
}