Pagini recente » Cod sursa (job #981475) | Cod sursa (job #457416) | Cod sursa (job #2062317) | Cod sursa (job #1948078) | Cod sursa (job #1471675)
#include <cstdio>
#include <algorithm>
#include <cstring>
#define NMAX 17007
using namespace std;
FILE *fin, *fout;
int n, m, k, pmd[NMAX], cost[NMAX], lvl[NMAX], tmp1, tmp2;
struct muchie
{
int a, b, c;
} v[NMAX];
int root(int x)
{
while(pmd[x] != x) x = pmd[x];
return x;
}
void level(int x)
{
if(pmd[x] == x) return;
level(pmd[x]);
lvl[x] = lvl[pmd[x]] + 1;
}
bool comp(muchie x, muchie y)
{
return x.c < y.c;
}
void citire()
{
scanf("%d %d %d", &n, &m, &k);
for(int i = 1; i<= m; ++i) scanf("%d %d %d", &v[i].a, &v[i].b, &v[i].c);
}
void arbore()
{
int rx, ry;
sort(v+1, v+m+1, comp);
for(int i = 1; i<= n; ++i) pmd[i] = i;
for(int i = 1; i<= m; ++i)
{
rx = root(v[i].a);
ry = root(v[i].b);
if(rx != ry)
{
pmd[rx] = ry;
cost[rx] = v[i].c;
}
}
}
void solve()
{
for(int i = 1; i<= n; ++i) if(lvl[i] == 0) level(i);
int maxn = 0;
for(int i = 1; i<= k; ++i)
{
scanf("%d %d", &tmp1, &tmp2);
maxn = 0;
while(lvl[tmp1] < lvl[tmp2])
{
maxn = max(maxn, cost[tmp2]);
tmp2 = pmd[tmp2];
}
while(lvl[tmp2] < lvl[tmp1])
{
maxn = max(maxn, cost[tmp1]);
tmp1 = pmd[tmp1];
}
while(tmp1 != tmp2)
{
maxn = max(maxn, cost[tmp2]);
maxn = max(maxn, cost[tmp1]);
tmp2 = pmd[tmp2];
tmp1 = pmd[tmp1];
}
printf("%d\n", maxn);
}
}
int main()
{
fin = freopen("radiatie.in", "r", stdin);
fout = freopen("radiatie.out", "w", stdout);
citire();
arbore();
solve();
fclose(fin);
fclose(fout);
return 0;
}