Pagini recente » tema | Cod sursa (job #3203249) | Cod sursa (job #2864782) | Cod sursa (job #1776682) | Cod sursa (job #379855)
Cod sursa(job #379855)
#include <algorithm>
using namespace std;
#define DIM 30005
int p[DIM>>1],r[DIM>>1],dst[DIM>>1],cst[DIM>>1],viz[DIM>>1];
struct muchie {int x,y,c;} v[DIM];
int n,m,k,nrm;
void read ()
{
int i;
scanf ("%d%d%d",&n,&m,&k);
for (i=1; i<=m; ++i)
scanf ("%d%d%d",&v[i].x,&v[i].y,&v[i].c);
}
int cmp (const muchie &a,const muchie &b)
{
return a.c<b.c;
}
int find (int x)
{
if (x!=p[x])
return find (p[x]);
return x;
}
void unite (int x,int y,int c)
{
if (r[x]<r[y])
{
p[x]=y;
cst[x]=c;
}
else
{
p[y]=x;
cst[y]=c;
}
if (r[x]==r[y])
++r[x];
}
void df (int nod)
{
viz[nod]=1;
if (p[nod]==nod)
dst[nod]=1;
else
{
if (!viz[p[nod]])
df (p[nod]);
dst[nod]=dst[p[nod]]+1;
}
}
void solve ()
{
int i;
sort (v+1,v+m+1,cmp);
for (i=1; i<=n; ++i)
p[i]=i;
for (i=1; nrm<n-1; ++i)
if (find (v[i].x)!=find (v[i].y))
{
++nrm;
unite (find (v[i].x),find (v[i].y),v[i].c);
}
for (i=1; i<=n; ++i)
if (!viz[i])
df (i);
}
void query ()
{
int i,x,y,nrmax;
for (i=1; i<=k; ++i)
{
scanf ("%d%d",&x,&y);
if (dst[x]<dst[y])
swap (x,y);
for (nrmax=0; dst[x]>dst[y]; x=p[x])
nrmax=max (nrmax,cst[x]);
for ( ; x!=y; x=p[x], y=p[y])
nrmax=max (nrmax, max (cst[x],cst[y]));
printf ("%d\n",nrmax);
}
}
int main ()
{
freopen ("radiatie.in","r",stdin);
freopen ("radiatie.out","w",stdout);
read ();
solve ();
query ();
return 0;
}