Pagini recente » Cod sursa (job #106918) | Cod sursa (job #1342279) | Cod sursa (job #156624) | Cod sursa (job #2591714) | Cod sursa (job #248730)
Cod sursa(job #248730)
#include <stdio.h>
#include <algorithm>
using namespace std;
struct per{int x;int y;int c;};
per a[30005];
long n,m,i,k,T,x,y,tx,ty,t[15005],d[15005],dmax[15005],v[15005];
int cmp(per n1, per n2){return (n1.c<n2.c);}
int main(){
freopen("radiatie.in","r",stdin);freopen("radiatie.out","w",stdout);
scanf("%ld %ld %ld",&n,&m,&T);
for (i=0;i<m;++i)scanf("%ld %ld %ld",&a[i].x,&a[i].y,&a[i].c);
sort(a,a+m,cmp);
for (i=0,k=1;k<n;i++){
tx=a[i].x;while (t[tx])tx=t[tx];
ty=a[i].y;while (t[ty])ty=t[ty];
if (tx==ty)continue;
t[ty]=tx;d[ty]=a[i].c;k++;
}
for (;T;--T){
scanf("%ld %ld",&x,&y);
v[x]=T;dmax[x]=0;
while (t[x]){
if (d[x]>dmax[x])dmax[t[x]]=d[x];
else dmax[t[x]]=dmax[x];
x=t[x];v[x]=T;
}
if (x==y)printf("%ld\n",dmax[x]);
else{
if (v[y]==T){printf("%ld\n",dmax[y]);continue;}
dmax[y]=0;
while (v[t[y]]!=T){
if (d[y]>dmax[y])dmax[t[y]]=d[y];
else dmax[t[y]]=dmax[y];
y=t[y];
}
if (d[y]>dmax[y])dmax[y]=d[y];
if (dmax[y]>dmax[t[y]]) printf("%ld\n",dmax[y]);
else printf("%ld\n",dmax[t[y]]);
}
}
return 0;
}