Cod sursa(job #2961237)

Utilizator matei.tudoseMatei Tudose matei.tudose Data 6 ianuarie 2023 00:02:42
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.48 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

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

struct muchie
{
    int start, end, cost;
};

struct nod
{
    int stramos, val_much_max;
};

int n, m, k, tata_de[30005], lantMax[30005], adancimi[30005];
nod stramosi[20][30005];
vector<muchie> graf, mstGraf[30005];

inline bool comparaCost(muchie x, muchie y)
{
    return x.cost < y.cost;
}

int findRoot(int x)
{
    if (x == tata_de[x])
        return x;
    return tata_de[x] = findRoot(tata_de[x]);
}

void join(int x, int y)
{
    int tata_x = findRoot(x);
    int tata_y = findRoot(y);
    if (tata_x == tata_y)
        return;
    else if (lantMax[tata_x] < lantMax[tata_y])
        tata_de[tata_x] = tata_y;
    else if (lantMax[tata_x] > lantMax[tata_y])
        tata_de[tata_y] = tata_x;
    else if (lantMax[tata_x] == lantMax[tata_y])
    {
        tata_de[tata_x] = tata_y;
        lantMax[tata_y]++;
    }
}

void mst()
{
    sort(graf.begin(), graf.end(), comparaCost);
    muchie it;
    for (int i = 0; i < graf.size(); i++)
    {
        it = graf[i];
        if (findRoot(it.start) != findRoot(it.end))
        {
            join(it.start, it.end);
            mstGraf[it.start].push_back(it);
            swap(it.start, it.end);
            mstGraf[it.end].push_back(it);
        }
    }
}

void DFS(int nod, int adancime)
{
    adancimi[nod] = adancime;
    for (int i = 0; i < mstGraf[nod].size(); i++)
    {
        muchie it = mstGraf[nod][i];
        if (adancimi[it.end] == 0)
        {
            stramosi[0][it.end].stramos = nod;
            stramosi[0][it.end].val_much_max = it.cost;
            DFS(it.end, adancime + 1);
        }
    }
}

void precalc()
{
    for (int i = 1; i <= n; i++)
        if (stramosi[0][i].stramos == 0)
            stramosi[0][i].stramos = i;
    for (int i = 1; i <= log2(n); i++)
        for (int j = 1; j <= n; j++)
        {
            stramosi[i][j].stramos = stramosi[i - 1][stramosi[i - 1][j].stramos].stramos;
            stramosi[i][j].val_much_max = max(stramosi[i - 1][j].val_much_max, stramosi[i - 1][stramosi[i - 1][j].stramos].val_much_max);
        }
}

void ans(int x, int y)
{
    if (adancimi[x] < adancimi[y])
        swap(x, y);
    int dist = adancimi[x] - adancimi[y];
    int ans = 0;
    for (int i = log2(dist); i >= 0 && dist; i--)
        if (dist >= (1 << i))
        {
            ans = max(ans, stramosi[i][x].val_much_max);
            x = stramosi[i][x].stramos;
            dist -= (1 << i);
        }
    if (x == y)
    {
        fout << ans << '\n';
        return;
    }
    for (int i = log2(n); i >= 0; i--)
        if (stramosi[i][x].stramos != stramosi[i][y].stramos)
        {
            ans = max(ans, stramosi[i][x].val_much_max);
            ans = max(ans, stramosi[i][y].val_much_max);
            x = stramosi[i][x].stramos;
            y = stramosi[i][y].stramos;
        }
    ans = max(ans, stramosi[0][x].val_much_max);
    ans = max(ans, stramosi[0][y].val_much_max);
    fout << ans << '\n';
}

int main()
{
    fin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
    {
        tata_de[i] = i;
        lantMax[i] = 1;
    }
    muchie it;
    for (int i = 0; i < m; i++)
    {
        fin >> it.start >> it.end >> it.cost;
        graf.push_back(it);
    }
    mst();
    for (int i = 1; i <= n; i++)
        if (adancimi[i] == 0)
            DFS(i, 0);
    precalc();
    int x, y;
    for (int i = 0; i < k; i++)
    {
        fin >> x >> y;
        ans(x, y);
    }
    return 0;
}