Pagini recente » Cod sursa (job #308513) | Cod sursa (job #3196173) | Cod sursa (job #2399600) | Cod sursa (job #288586) | Cod sursa (job #901338)
Cod sursa(job #901338)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define forit(it, v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
const int MAXN = 16000;
int N, M, K;
int level[MAXN], dad[MAXN], adan[MAXN], cost[MAXN];
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
struct edge {
int x, y, cost;
};
edge make_edge(int x, int y, int cost) {
edge a;
a.x = x;
a.y = y;
a.cost = cost;
return a;
}
bool cmp(edge a, edge b) {
return a.cost < b.cost;
}
int find_dad(int node) {
adan[node] = 0;
int nod = node;
for (nod = node; nod != dad[nod]; nod = dad[nod], ++adan[node]);
return nod;
}
void uneste (int nod1, int nod2, int c) {
if (level[nod1] <= level[nod2]) {
dad[nod1] = nod2;
if (level[nod1] == level[nod2])
++level[nod2];
cost[nod1] = c;
}
else {
dad[nod2] = nod1;
cost[nod2] = c;
}
}
inline void initializare_paduri() {
for (int i = 1; i <= N; ++i) {
dad[i] = i;
adan[i] = 1;
}
}
vector <edge> edges;
int main() {
fin >> N >> M >> K;
for (int i = 1; i <= M; ++i) {
int x, y, cost;
fin >> x >> y >> cost;
edges.push_back(make_edge(x, y, cost));
}
sort (edges.begin(), edges.end(), cmp);
initializare_paduri();
forit(it, edges) {
if (find_dad(it->x) != find_dad(it->y))
uneste(find_dad(it->x), find_dad(it->y), it->cost);
}
for (int i = 1; i <= N; ++i)
find_dad(i);
for (int i = 1; i <= K; ++i) {
int a, b;
fin >> a >> b;
int rez = 0;
if (adan[a] < adan[b]) swap(a, b);
while (adan[a] > adan[b]) {
rez = max(rez, cost[a]);
a = dad[a];
}
while (a != b) {
rez = max (rez, cost[a]);
rez = max (rez, cost[b]);
a = dad[a];
b = dad[b];
}
fout << rez << "\n";
}
return 0;
}