Cod sursa(job #1512765)

Utilizator andrei.arnautuAndi Arnautu andrei.arnautu Data 28 octombrie 2015 16:43:32
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.95 kb
/**
  *  Worg
  */
#include <queue>
#include <cstdio>
#include <vector>
#include <algorithm>

#define pb          push_back

using namespace std;
FILE *fin = freopen("radiatie.in", "r", stdin);
FILE *fout = freopen("radiatie.out", "w", stdout);

const int MAX_N = 1 + 15000,
          bufferDim = 10000;

class inputReader {

private:
        int pos;
        char buffer[bufferDim];
public:

        void getBuffer() {

            pos= 0;
            fread(buffer, 1, bufferDim, stdin);
        }

        bool digit(char c) {

            return ('0' <= c && c <= '9');
        }

        void getInt(int &nr) {

            while(!digit(buffer[pos]))
                if(++pos == bufferDim)
                    getBuffer();
            nr = 0;
            while(digit(buffer[pos])) {

                nr = nr * 10 + buffer[pos] - '0';
                if(++pos == bufferDim)
                    getBuffer();
            }
        }
} cin;

struct edge {

    int a, b, cost;

    edge(const int &a, const int &b, const int &cost) {

        this->a = a, this->b = b, this->cost = cost;
    }

    bool operator <(const edge &other) const {

        return this->cost < other.cost;
    }
};
/****************end of class and struct declarations ****************/
vector < edge > candidates;

bool solved[MAX_N];
int father[MAX_N], depth[MAX_N], cost[MAX_N];
int n, m, k;

int group(int x) {

    if(x != father[x])
        x = group(father[x]);
    return x;
}

void getDepth(int node) {

    if(node == father[node])
        return;
    getDepth(father[node]);
    depth[node] = depth[father[node]] + 1;
}

void readGraph() {

    cin.getBuffer();
    cin.getInt(n); cin.getInt(m); cin.getInt(k);

    for(int i = 1; i <= m; ++i) {

        int a, b, cost;
        cin.getInt(a); cin.getInt(b); cin.getInt(cost);
        candidates.pb(edge(a, b, cost));
    }
}

void getAPM() {

    sort(candidates.begin(), candidates.end());
    for(int i = 1; i <= n; ++i)
        father[i] = i;

    for(int i = 0; i < m; ++i) {

        int x = group(candidates[i].a), y = group(candidates[i].b);
        if(x != y) {

            father[x] = y;
            cost[x] = candidates[i].cost;
        }
    }
    for(int i = 1; i <= m; ++i)
        if(!depth[i])
            getDepth(i);
}

int query(int x, int y) {

    int maxCost = 0;

    while(depth[x] < depth[y]) {

        maxCost = max(maxCost, cost[y]);
        y = father[y];
    }
    while(depth[y] < depth[x]) {

        maxCost = max(maxCost, cost[x]);
        x = father[x];
    }
    while(x != y) {

        maxCost = max(maxCost, max(cost[x], cost[y]));
        x = father[x];
        y = father[y];
    }
    return maxCost;
}

int main() {

    readGraph();
    getAPM();
    for(; k; --k) {

        int x, y;
        cin.getInt(x); cin.getInt(y);
        printf("%d\n", query(x, y));
    }
    return 0;
}