Pagini recente » Cod sursa (job #1951233) | Cod sursa (job #2269855) | Cod sursa (job #1974737) | Cod sursa (job #1224488) | Cod sursa (job #1160885)
/*
Keep It Simple!
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include<cstdio>
#include<algorithm>
#define MaxN 15005
#define MaxM 30005
#define MaxV(a,b) ((a)>(b)?(a):(b))
using namespace std;
struct drum
{
int x, y, c;
}roads[MaxM];
int T[MaxN];
int C[MaxN];
int Rg[MaxN];
bool Use[MaxN];
int Level[MaxN];
int N, M, K;
bool cmp(drum First, drum Second)
{
return First.c < Second.c;
}
int Find(int node)
{
for (node; T[node] != node; node = T[node]);
return node;
}
void Unite(int x, int y,int cost)
{
if (Rg[x] > Rg[y])
{
T[y] = x;
C[y] = cost;
}
else
{
T[x] = y;
C[x] = cost;
}
if (Rg[x] == Rg[y]) Rg[x]++;
}
void InitLevel(int node)
{
if (T[node] == node)
Level[node] = 1;
else
{
if (!Level[T[node]])
InitLevel(T[node]);
Level[node] = Level[T[node]] + 1;
}
}
int main()
{
freopen("radiatie.in", "r", stdin);
freopen("radiatie.out", "w", stdout);
scanf("%d%d%d", &N, &M, &K);
for (int i = 1; i <= M; i++)
scanf("%d%d%d", &roads[i].x, &roads[i].y, &roads[i].c);
sort(roads + 1, roads + M + 1, cmp);
for (int i = 1; i <= N; i++)
{
T[i] = i;
Rg[i] = 1;
}
for (int i = 1; i <= M; i++)
if (T[roads[i].x] == roads[i].x || T[roads[i].y] == roads[i].y)
Unite(roads[i].x, roads[i].y, roads[i].c);
for (int i = 1; i <= N;i++)
if (!Level[i])
InitLevel(i);
int x, y;
for (int i = 1; i <= K; i++)
{
scanf("%d%d", &x, &y);
int maxvalue = -1;
while (x != y)
{
if (Level[x] > Level[y])
{
maxvalue = MaxV(maxvalue, C[x]);
x = T[x];
}
else
{
maxvalue = MaxV(maxvalue, C[y]);
y = T[y];
}
}
printf("%d\n", maxvalue);
}
}