Pagini recente » Cod sursa (job #1758631) | Cod sursa (job #1068898) | Cod sursa (job #185721) | Cod sursa (job #2336730) | Cod sursa (job #1330132)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#define nmax 15005
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
struct muchie {
int x, y, cost;
} M[2*nmax];
int n, m, k, q = 0;
vector < pair<int, int> > V[nmax];
void citire() {
int a, b, c;
fin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
fin >> a >> b >> c;
M[i].x = a;
M[i].y = b;
M[i].cost = c;
}
}
int R[nmax], NR[nmax];
bool cmp(muchie a, muchie b) {
return a.cost < b.cost;
}
int root(int nod) {
if (R[nod] == nod)
return nod;
return root(R[nod]);
}
void kruskal() {
int i;
for (i = 1; i <= n; i++)
NR[i] = 1, R[i] = i;
int nrm = 0;
sort(M+1,M+m+1,cmp);
for (i = 1; i <= m; i++) {
int p1 = root(M[i].x);
int p2 = root(M[i].y);
if (p1 == p2)
continue;
if (NR[p1] >= NR[p2]) {
NR[p1] += NR[p2];
R[p2] = p1;
} else {
NR[p2] += NR[p1];
R[p1] = p2;
}
nrm++;
V[M[i].x].push_back(make_pair(M[i].y, M[i].cost));
V[M[i].y].push_back(make_pair(M[i].x, M[i].cost));
if (nrm == n-1)
break;
}
}
int e;
bool viz[nmax];
int P[nmax], Min[18][2*nmax], Exp[2*nmax];
void euler(int nod) {
viz[nod] = true;
++e;
P[nod] = e;
for (int i = 0; i < V[nod].size(); i++)
if (!viz[V[nod][i].first]) {
Min[0][++q] = V[nod][i].second;
euler(V[nod][i].first);
++e;
Min[0][++q] = V[nod][i].second;
}
}
void preprocess() {
int l = e - 1;
for (int i = 1; (1<<i) <= l; i++)
for (int j = 1; j <= l - (1<<i) + 1; j++) {
int dr = j + (1<<i) - 1;
int mid = dr - (1<<(i-1)) + 1;
Min[i][j] = max(Min[i-1][j], Min[i-1][mid]);
}
}
void solve() {
euler(1);
preprocess();
Exp[1] = 0;
for (int i = 2; i <= e; i++)
Exp[i] = Exp[i/2] + 1;
for (int i = 1; i <= k; i++) {
int x, y;
fin >> x >> y;
x = P[x], y = P[y];
if (x > y) swap(x, y);
int l = y - x + 1;
int k = Exp[l];
int rez = Min[k][x];
x = y - (1<<k) + 1;
rez = max(rez, Min[k][x]);
fout << rez << "\n";
}
}
int main() {
citire();
kruskal();
solve();
fin.close();
fout.close();
return 0;
}