Pagini recente » Cod sursa (job #1854261) | Cod sursa (job #1554913) | Cod sursa (job #2431936) | Cod sursa (job #970164) | Cod sursa (job #442971)
Cod sursa(job #442971)
#include <stdio.h>
#include <algorithm>
#define Nmax 15005
#define Mmax 30005
#define INF 2147000000
using namespace std;
int a[Mmax],b[Mmax],c[Mmax],ind[Mmax];
int gr[Nmax],tt[Nmax];
int h[Nmax],cost[Nmax];
int n,m,k,i,rez,x,y,ta,tb;
inline int Maxim(int x,int y){ return x>y ? x:y; }
inline int cmp(int i,int j){
return c[i] < c[j];
}
inline int Find(int x){
int r;
for(r=x; r!=tt[r]; r=tt[r]);
return r;
}
inline void dfs(int x){
if(x==tt[x]) h[x]=1;
else{
dfs(tt[x]);
h[x]=h[tt[x]]+1;
}
}
int main(){
freopen("radiatie.in","r",stdin);
freopen("radiatie.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=m;++i)
scanf("%d%d%d",&a[i],&b[i],&c[i]), ind[i]=i;
sort(ind+1,ind+m+1,cmp);
for(i=1;i<=n;++i) tt[i]=i, gr[i]=1;
for(i=1;i<=m;++i){
ta = Find(a[ind[i]]); tb = Find(b[ind[i]]);
if( ta != tb ){
tt[ta]=tb;
cost[ta]=c[ind[i]];
}
}
for(i=1;i<=n;++i)
if(!h[i])
dfs(i);
for(i=1;i<=k;++i){
scanf("%d%d",&x,&y);
rez=0;
while( h[x] > h[y] ) {
rez=Maxim(rez,cost[x]);
x=tt[x];
}
while( h[y] > h[x] ) {
rez=Maxim(rez,cost[y]);
y=tt[y];
}
while( x != y ){
rez=Maxim(rez,cost[x]);
rez=Maxim(rez,cost[y]);
x=tt[x];
y=tt[y];
}
printf("%d\n",rez);
}
fclose(stdin); fclose(stdout);
return 0;
}