Pagini recente » Cod sursa (job #273991) | Cod sursa (job #1249562) | Cod sursa (job #233443) | Cod sursa (job #1990760) | Cod sursa (job #545992)
Cod sursa(job #545992)
#include<stdio.h>
#define Nmax 15010
#define Mmax 30010
#define max(a,b) a>b?a:b
#include<algorithm>
using namespace std;
int tt[ Nmax ],mm[ Nmax ],pp[ Nmax ],nr[ Nmax ],mmax[ Nmax ],i,j,k,l,m,n;
struct muchie {int a,b,c;} v[ Mmax ];
struct cmp{
bool operator()(const muchie &a,const muchie &b){
return a.c < b.c;
}
};
int tata(int x){
while(tt[x] != x )
x = tt[x];
return x;
}
void querry(int x,int k){
int y;
mm[x] =0;
pp[x] = k;
while(tt[x] != x)
{y = tt[x];
mm[y] = max(mm[x] , mmax[x]);
pp[y] = k;
x = tt[x];
}
}
int search(int x,int k){
int sol =mmax[x];
if(pp[x] == k)
return mm[x];
while(x != tt[x])
{if(pp[tt[x]] == k){
sol = max(mm[tt[x]],mmax[x]);
return sol;
}
sol = max(mmax[x],sol);
x = tt[x];
}
return sol;
}
void citire(){
freopen("radiatie.in","r",stdin);
freopen("radiatie.out","w",stdout);
scanf("%d %d %d",&n,&m,&k);
int i,a,b,c;
for(i =1 ;i <= m ; i++)
{scanf("%d %d %d",&a,&b,&c);
v[i].a = a;
v[i].b = b;
v[i].c = c;
}
for ( i = 1; i <= n; i++){
tt[i] = i;
nr[i] = 1;
}
sort(v+1,v+1+m,cmp());
for(i = 1; i <= m ;i++)
{a = tata(v[i].a);
b = tata(v[i].b);
if(a != b)
{if(nr[ a ] > nr[ b ] ){
tt[b] = a;
mmax[ b ] = v[i].c;
nr[ a ] += nr[ b ];
}
else
{tt[a] = b;
mmax[ a ] = v[ i ].c;
nr[ b ] += nr[ a ];
}
}
}
int sol;
for(i =1 ; i <= k ; i++)
{scanf("%d %d",&a,&b);
querry(b,i);
sol = search(a,i);
printf("%d\n",sol);
}
}
int main(){
citire();
return 0;}