Cod sursa(job #3279165)

Utilizator NonnonsniperCretu Marian-Dumitru Nonnonsniper Data 21 februarie 2025 23:07:19
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda lasm_21_02_2025_clasa11 Marime 3.27 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int m, k, L[105], maxim;
short n, q, t[15005], v[31005], nivel[31005], e[31005], poz[15005];
pair<short, short> rmq[15][31005];
pair<short, int> pred[15005];
vector< pair<short, int> > G[15005];
bool viz[15005];
struct Muchie{
    short x, y;
    int cost;
    bool operator <(const Muchie e) const
    {
        return cost < e.cost;
    }
}a[100005];
void Union(short x, short y)
{
    t[y] = x;
}
short Find(short x)
{
    short rad, y;
    rad = x;
    while(t[rad] != 0)
        rad = t[rad];
    while(x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}
void Kruskal()
{
    short x, y, nrc;
    nrc = n;
    for(int i = 1;i <= m && nrc > 1;i++)
    {
        x = Find(a[i].x);
        y = Find(a[i].y);
        if(x != y)
        {
            nrc--;
            Union(y, x);
            G[x].push_back({y, a[i].cost});
            G[y].push_back({x, a[i].cost});
        }
    }
}
void Dfs(short x, short dist)
{
    short nod;
    int cost;
    viz[x] = 1;
    v[++k] = x;
    nivel[k] = dist;
    for(auto e : G[x])
    {
        nod = e.first;
        cost = e.second;
        if(viz[nod] == 0)
        {
            Dfs(nod, dist + 1);
            pred[nod] = {x, cost};
            v[++k] = x;
            nivel[k] = dist;
        }
    }
}
void LCA()
{
    int i, j;
    Dfs(1, 0);
    e[1] = 0;
    for(i = 2;i <= k;i++)
        e[i] = 1 + e[i / 2];
    L[0] = 1;
    for(i = 1;i <= 20;i++)
        L[i] = 2 * L[i - 1];
    for(i = 1;i <= k;i++)
        if(poz[v[i]] == 0) poz[v[i]] = i;
    for(i = 1;i <= k;i++)
    {
        rmq[0][i].first = nivel[i];
        rmq[0][i].second = v[i];
    }
    for(j = 1;j <= e[k];j++)
        for(i = 1;i <= k - L[j] + 1;i++)
            if(rmq[j - 1][i].first <= rmq[j - 1][i + L[j - 1]].first)
                rmq[j][i] = rmq[j - 1][i];
            else rmq[j][i] = rmq[j - 1][i + L[j - 1]];
}

int main()
{
    short i, j, c, x, mini, maxi, nod;
    pair<short, short> sol;
    fin >> n >> m >> q;
    for(int ind = 1;ind <= m;ind++)
    {
        fin >> i >> j >> c;
        a[ind].x = i;
        a[ind].y = j;
        a[ind].cost = c;
    }
    sort(a + 1, a + m + 1);
    Kruskal();
    LCA();
    while(q)
    {
        fin >> i >> j;
        mini = min(poz[i], poz[j]);
        maxi = max(poz[i], poz[j]);
        x = e[maxi - mini + 1];
        if(rmq[x][mini].first <= rmq[x][maxi - L[x] + 1].first)
            sol = rmq[x][mini];
        else sol = rmq[x][maxi - L[x] + 1];
        nod = sol.second;
        maxim = 0;
        if(nod != i)
        {
            while(pred[i].first != nod)
            {
                maxim = max(maxim, pred[i].second);
                i = pred[i].first;
            }
            maxim = max(maxim, pred[i].second);
        }
        if(nod != j)
        {
            while(pred[j].first != nod)
            {
                maxim = max(maxim, pred[j].second);
                j = pred[j].first;
            }
            maxim = max(maxim, pred[j].second);
        }
        fout << maxim << "\n";
        q--;
    }
    fout.close();
    return 0;