Pagini recente » Cod sursa (job #2779004) | Cod sursa (job #2090687) | Cod sursa (job #420446) | Cod sursa (job #2271605) | Cod sursa (job #497186)
Cod sursa(job #497186)
#include<fstream>
#include<vector>
#define dmax 30008
using namespace std;
ifstream in("radiatie.in");
ofstream out("radiatie.out");
int n,m,q,x,y,f[dmax],h[dmax];
long int c[dmax],mf[dmax];
bool t[dmax];
struct muchie
{ int a;
int b;
long int c;
} g[dmax];
int sf(struct muchie p, struct muchie r)
{ return p.c <= r.c;
}
int acc(int x, int y)
{ while( f[x])
x=f[x];
while(f[y])
y=f[y];
if(x==y)
return 1;
return 0;
}
void uneste(int x, int y,int cst)
{
if(h[x] < h[y])
{ f[x]=y;
c[x]=cst;
h[x]=h[y]+1;
}
else if(h[y] < h[x])
{ f[y]=x;
c[y]=cst;
h[y]=h[x]+1;
}
else if(h[x] == h[y])
{ f[y]=x;
c[y]=cst;
h[x]++;
}
}
void kruskal()
{ int nr,i;
sort( g+1, g+m+1, sf );
nr=0;
for(i=1; nr<n-1; i++)
{
if( !acc(g[i].a, g[i].b) )
{ nr++;
uneste(g[i].a, g[i].b, g[i].c);
}
}
}
void go(int a,int b)
{ int mx=-1,aa,bb;
aa=a, bb=b;
while( f[a])
{ mf[f[a]]=max(mf[a], c[a]);
a=f[a];
}
while(f[b] && ! mf[f[b]])
{ mf[f[b]]=max(mf[b], c[b]);
b=f[b];
}
mf[f[b]]= max(mf[f[b]], mf[b]);
mf[f[b]]= max(mf[f[b]], c[b]);
out<<mf[f[b]]<<'\n';
while(mf[f[aa]])
{ aa=f[aa];
mf[aa]=0;
}
while(mf[f[bb]])
{ bb=f[bb];
mf[bb]=0;
}
}
int main()
{ int i,j;
in>>n>>m>>q;
for(i=1;i<=m;i++)
in>>g[i].a>>g[i].b>>g[i].c;
for(i=1;i<=n;i++)
h[i]=1;
kruskal();
for(i=1;i<=q;i++)
{ in>>x>>y;
go(x,y);
}
in.close();
out.close();
return 0;
}