Pagini recente » Cod sursa (job #2225526) | Cod sursa (job #1599666) | Cod sursa (job #1740041) | Cod sursa (job #2901253) | Cod sursa (job #2154277)
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("radiatie.in");
ofstream out("radiatie.out");
const int NMAX = 15000;
struct Data {
int x;
int y;
int c;
bool operator< (Data other) const {
return c < other.c;
}
};
int n, m, k;
int p[1 + NMAX];
int r[1 + NMAX];
int cost[1 + NMAX];
int used[1 + NMAX];
Data g[1 + 2 * NMAX];
int src(int x) {
while(p[x] != x)
x = p[x];
return x;
}
void combine(int x, int y, int c) {
if(r[x] < r[y]) {
p[x] = y;
cost[x] = c;
} else {
p[y] = x;
cost[y] = c;
}
if(r[x] == r[y])
r[x]++;
}
void compute_arb() {
sort(g + 1, g + m + 1);
for(int i = 1; i <= n; i++)
p[i] = i;
for(int i = 1; i <= m; i++) {
int a = src(g[i].x);
int b = src(g[i].y);
if(a != b)
combine(a, b, g[i].c);
}
}
int compute_lca(int x, int y, int q) {
while(p[x] != x ) {
used[x] = q;
x = p[x];
}
while(p[y] != y && used[y] != q) {
y = p[y];
}
return y;
}
int get_max(int x, int y) {
int ct = 0;
while(x != y) {
ct = max(ct, cost[x]);
x = p[x];
}
return ct;
}
int query(int x, int y, int q) {
int lca = compute_lca(x, y, q);
return max(get_max(x, lca), get_max(y, lca));
}
int main()
{
in >> n >> m >> k;
for(int i = 1; i <= m; i++)
in >> g[i].x >> g[i].y >> g[i].c;
compute_arb();
for(int q = 1; q <= k; q++) {
int x, y;
in >> x >> y;
out << query(x, y, k - q + 1) << '\n';
}
in.close();
out.close();
return 0;
}