Pagini recente » Cod sursa (job #479796) | Cod sursa (job #2306192) | Cod sursa (job #2041411) | Cod sursa (job #1412125) | Cod sursa (job #3222688)
using namespace std;
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
#define MMAX 300005
#define NMAX 15005
int n, m, nrq;
struct Muchie{
int x, y, c;
} muc[MMAX];
vector<pair<int,int>> G[NMAX];
int k, a, b;
int t[NMAX], h[NMAX];
int c[NMAX];
int rad(int nod) {
while (nod != t[nod]) {
nod = t[nod];
}
return nod;
}
void Union(int x, int y, int cost) {
int radx = rad(x), rady = rad(y);
if (h[radx] < h[rady]) {
t[radx] = rady;
c[radx] = cost;
h[rady] += h[radx];
} else {
t[rady] = radx;
c[rady] = cost;
h[radx] += h[rady];
}
}
void APM();
int main() {
fin >> n >> m >> nrq;
for (int i = 1; i<=m; i++) {
fin >> muc[i].x >> muc[i].y >> muc[i].c;
}
for (int i = 1; i<=n; i++) {
t[i] = i;
h[i] = 1;
}
APM();
for (int i = 1; i<=nrq; i++) {
fin >> a >> b;
int rada = a, niva = 0;
while (rada != t[rada]) {
rada = t[rada];
niva++;
}
int radb = b, nivb = 0;
while (radb != t[radb]) {
radb = t[radb];
nivb++;
}
if (niva < nivb) {
swap(a, b);
swap(niva, nivb);
}
int sol = 0;
while (niva > nivb) { ///il aduc pe a la acelasi nivel
sol = max(sol, c[a]);
a = t[a];
niva--;
}
while (a != b) {
sol = max(sol, max(c[a], c[b])); ///parcurg restul muchiilor
a = t[a], b = t[b];
}
fout << sol << "\n";
}
return 0;
}
bool cmp(Muchie a, Muchie b) {
return a.c < b.c;
}
void APM() {
sort(muc+1, muc+m+1, cmp);
for (int i = 1; i<=m; i++) {
if (rad(muc[i].x) != rad(muc[i].y)) {
Union(muc[i].x, muc[i].y, muc[i].c);
}
}
}