Pagini recente » Cod sursa (job #2477891) | Cod sursa (job #2496107) | clasament-teme | Cod sursa (job #221339) | Cod sursa (job #503583)
Cod sursa(job #503583)
#include <stdio.h>
#include <algorithm>
using namespace std;
#define nmax 32397
int a[nmax], b[nmax], c[nmax], tata[nmax], ind[nmax], viz[nmax], cost[nmax];
int n, m, t;
struct cmp
{
bool operator() (const int &a, const int & b) const
{
return c[a]<c[b];
}
};
void citire()
{
int i;
freopen("radiatie.in","r",stdin);
freopen("radiatie.out","w",stdout);
scanf("%d %d %d", &n, &m, &t);
for (i = 1; i <= m; ++i)
{
scanf("%d %d %d", &a[i], &b[i], &c[i]);
ind[i] = i;
}
for (i=1;i<=n;++i) tata[i]=i;
sort(ind+1, ind+m+1, cmp());
}
int father(int x)
{
if(x==tata[x])
return x;
return father(tata[x]);
}
void unire(int i, int j)
{
tata[i]=j;
}
void dfs(int nod)
{
if (tata[nod]==nod)
{
viz[nod]=1;
return ;
}
dfs(tata[nod]);
viz[nod]=viz[tata[nod]]+1;
}
int query(int x, int y)
{
int c = 0;
if (viz[x]<viz[y])
swap(x,y);
while(viz[x]>viz[y])
{
c=max(c,cost[x]);
x=tata[x];
}
while(x!=y)
{
c=max(c,cost[x]);
c=max(c,cost[y]);
x=tata[x];
y=tata[y];
}
return c;
}
void solve()
{
int i,t1,t2,x,y;
for(i=1;i<=m;i++)
{
t1=father(a[ind[i]]);
t2=father(b[ind[i]]);
if(t1!=t2)
{
unire(t1,t2);
cost[t1]=c[ind[i]];
}
}
for (i=1;i<=n;++i)
if (!viz[i])
dfs(i);
while (t--)
{
scanf("%d %d", &x,&y);
printf("%d\n", query(x,y));
}
}
int main()
{
citire();
solve();
return 0;
}