Cod sursa(job #1160971)

Utilizator taigi100Cazacu Robert taigi100 Data 30 martie 2014 22:19:17
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
/*
	Keep It Simple!
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<cstdio>
#include<algorithm>
#include<vector>

#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 Level[MaxN];

int N, M, K;

vector<int> G[MaxN];

bool cmp(drum First, drum Second)
{
	return First.c < Second.c;
}

int Find(int node)
{
	for (; T[node] != node; node = T[node]);
	return node;
}

void Unite(int x, int y,int cost)
{
		T[x] = y;
		C[x] = cost;
		G[y].push_back(x);
}

void DFS(int node)
{
	for (size_t i = 0; i < G[node].size(); i++)
	{
		Level[G[node][i]] = Level[node] + 1;
		DFS(G[node][i]);
	}
}

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;

	for (int i = 1; i <= M; i++)
		if ( Find(roads[i].x) != Find(roads[i].y))
			Unite(Find(roads[i].x), Find(roads[i].y), roads[i].c);

		int root = -1;
		for (int i = 1; i <= N;i++)
		if (T[i] == i)
		{
			root = i;
			break;
		}
		Level[root] = 1;
			
		DFS(root);

		int x, y;

		for (int i = 1; i <= K; i++)
		{
			scanf("%d%d", &x, &y);

			int maxvalue = -1;
				while (Level[x] > Level[y])
				{
					maxvalue = MaxV(maxvalue, C[x]);
					x = T[x];
				}
				while (Level[y] > Level[x])
				{
					maxvalue = MaxV(maxvalue, C[y]);
					y = T[y];
				}

				while (x != y)
				{
					maxvalue = MaxV(maxvalue, MaxV(C[y], C[x]));
					x = T[x];
					y = T[y];
				}

			printf("%d\n", maxvalue);
		}

}