Cod sursa(job #389120)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 31 ianuarie 2010 22:29:21
Problema Radiatie Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include<cstdio>
#define N 15001
#define M 2*N-1
#include<algorithm>
#include<vector>
#define maxim(a,b) ((a<b)?(b):(a))
using namespace std;
short int n,m,k,x1,y1;
int ind[M];
/*vector<short int>x(M),y(M),gr(N),dist(N,0);
vector<int>c(N),z(M);*/
short int x[M],y[M],gr[N],dist[N];
int c[N],z[M];
bool cmp(const int &i,const int &j)
{
	return z[i]<z[j];
}
short int find(int x)
{
	if (gr[x]==x)
		return gr[x];
	return find(gr[x]);
}
void apm()
{
	for (int i=1; i<=n; ++i)
		gr[i]=i;
	short int num=0;
	--n;
	for (int i=1; i<=m&&num!=n; ++i)
	{
		short int u=find(x[ind[i]]),v=find(y[ind[i]]);
		if (u!=v)
		{
			gr[u]=v;
			c[u]=z[ind[i]];
		}
	}
}
void df(int x)
{
	if (gr[x]==x)
	{
		dist[x]=1;
		return;
	}
	df(gr[x]);
	dist[x]=1+dist[gr[x]];
}

int solve()
{
	int rez=0;
	if (dist[x1]<dist[y1])
		swap(x1,y1);
	while (dist[x1]>dist[y1])
	{
		rez=maxim(rez,c[x1]);
		x1=gr[x1];
	}
	while (x1!=y1)
	{
		rez=maxim(rez,c[x1]);
		rez=maxim(rez,c[y1]);
		x1=gr[x1];
		y1=gr[y1];
	}
	return rez;
}
void citire()
{
	freopen("radiatie.in","r",stdin);
	freopen("radiatie.out","w",stdout);
	scanf("%hd%hd%hd",&n,&m,&k);
	for (int i=1; i<=m; ++i)
	{
		scanf("%hd%hd%d",&x[i],&y[i],&z[i]);
		ind[i]=i;
	}
	sort(ind+1,ind+1+m,cmp);
	apm();
	for (int i=1; i<=n; ++i)
		if (!dist[i])
			df(i);
	/*for (int i=1; i<=n; ++i)
	{
		printf("%d\n",dist[i]);
	}*/
	for (int i=1; i<=k; ++i)
	{
		scanf("%hd%hd",&x1,&y1);
		printf("%d\n",solve());
	}
}
int main()
{
	citire();
	return 0;
}