Pagini recente » Cod sursa (job #2912527) | Cod sursa (job #1850374) | Cod sursa (job #2367123) | Cod sursa (job #34185) | Cod sursa (job #777390)
Cod sursa(job #777390)
#include <stdio.h>
#include <algorithm>
using namespace std;
int lastWalk[15001], costToParent[15001], rank[15001], parent[15001];
void Union(int v1, int v2, int cost) {
if (rank[v1] > rank[v2]) {
parent[v2] = v1;
costToParent[v2] = cost;
}
else {
parent[v1] = v2;
costToParent[v1] = cost;
if (rank[v1] == rank[v2])
rank[v2]++;
}
}
int Find(int vertex) {
if (parent[vertex] != 0)
return Find(parent[vertex]);
return vertex;
}
int query(int a, int b, int color) {
int max = 0, first = a;
while (parent[a] != 0) {
lastWalk[a] = color;
a = parent[a];
}
while (parent[b] != 0 && lastWalk[b] != color) {
if (max < costToParent[b])
max = costToParent[b];
b = parent[b];
}
while (first != b) {
if (max < costToParent[first])
max = costToParent[first];
first = parent[first];
}
return max;
}
struct edge {
unsigned short a, b;
int c;
};
struct cmp
{
bool operator()(const edge &a, const edge &b)const
{
if(a.c < b.c) return 1;
return 0;
}
};
int main() {
int N, i, M, K;
edge m[15001 << 1];
FILE *input, *output;
input = fopen("radiatie.in", "r");
output = fopen("radiatie.out", "w");
fscanf(input, "%d %d %d", &N, &M, &K) ;
for (i = 1; i <= M; ++i)
fscanf(input, "%d %d %d", &m[i].a, &m[i].b, &m[i].c);
sort(m + 1, m + M + 1, cmp());
for(i = 1; i <= M; ++i) {
if(Find(m[i].a) != Find(m[i].b)) {
Union(m[i].a, m[i].b, m[i].c);
}
}
for (int i = 1 ; i <= K ; ++i) {
int a, b;
fscanf(input, "%d %d", &a, &b);
fprintf(output, "%d\n", query(a, b, i));
}
fclose(input);
fclose(output);
return 0;
}