Cod sursa(job #1009747)

Utilizator FlameingoAiordachioaei Marius Flameingo Data 13 octombrie 2013 19:18:48
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <cstdio>
#include <algorithm>
using namespace std;

const int NMAX = 15003, MMAX = 30003, INFI = 2e9;

int father[NMAX], cost[NMAX], max_depth[NMAX], edge[MMAX], node1[MMAX], node2[MMAX], c[MMAX];

inline bool compare (const int &a, const int &b) {

    return c[a] < c[b];

}

inline int root (int node) {

    for (; father[node] != node; node = father[node]);
    return node;

}

inline void unite (const int &root, const int &_root, const int &a_cost) {

    if (max_depth[root] < max_depth[_root])
        father[root] = _root,
        cost[root] = a_cost;
    else
        father[_root] = root,
        cost[_root] = a_cost;
    if (max_depth[root] == max_depth[_root])
        ++max_depth[root];

}

int main () {

    freopen ("radiatie.in", "r", stdin);
    freopen ("radiatie.out", "w", stdout);
    int N, M, K, i, x, y, _lca, R;
    scanf ("%d%d%d", &N, &M, &K);
    for (i = 1; i <= N; ++i)
        father[i] = i;
    for (i = 1; i <= M; ++i)
        scanf ("%d%d%d", node1 + i, node2 + i, c + i),
        edge[i] = i;
    sort (edge + 1, edge + M + 1, compare);
    for (i = 1; i <= M; ++i) {
        x = root (node1[edge[i]]);
        y = root (node2[edge[i]]);
        if (x != y)
            unite (x, y, c[edge[i]]);
    }
    while (K--) {
        scanf ("%d%d", &x, &y);
        R = 0;
        while (x != y)
            if (max_depth[x] < max_depth[y])
                R = max (R, cost[x]),
                x = father[x];
            else
                R = max (R, cost[y]),
                y = father[y];
        printf ("%d\n", R);
    }

}