Pagini recente » Cod sursa (job #160865) | Cod sursa (job #829191) | Cod sursa (job #793596) | Cod sursa (job #1019151) | Cod sursa (job #3335786)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f ("radiatie.in");
ofstream g ("radiatie.out");
const int NMAX = 15000,
LOG = 15;
int N, K, M, CC[NMAX+1],
depth[NMAX+1], maxM[NMAX+1][LOG+1], up[NMAX+1][LOG+1];
struct muchie {
int x, y, c;
//
muchie(int xx = 0, int yy = 0, int cc = 0) : x(xx), y(yy), c(cc) {}
//
bool operator <(const muchie &m) const {
return c < m.c;
}
};
vector <muchie> Muchii;
struct muchieAPM {
int y, c;
//
muchieAPM(int yy = 0, int cc = 0) : y(yy), c(cc) {}
};
vector <muchieAPM> G[NMAX+1];
void citire() {
int x, y, c;
f >> N >> M >> K;
for (int i=1; i<=M; i++) {
f >> x >> y >> c;
Muchii.push_back(muchie(x, y, c));
}
}
int Find(int i) {
if (CC[i] == 0) return i;
return CC[i] = Find(CC[i]);
}
void Kruskal() {
int nrM = 0;
//
sort(Muchii.begin(), Muchii.end());
//
for(const auto &m : Muchii) {
int cx = Find(m.x),
cy = Find(m.y);
//
if (cx != cy) {
CC[cy] = cx;
G[m.x].push_back(muchieAPM(m.y, m.c));
G[m.y].push_back(muchieAPM(m.x, m.c));
nrM++;
}
//
if (nrM == N-1) break;
}
}
void DFS(int nod, int tata, int cost) {
depth[nod] = depth[tata] + 1;
up[nod][0] = tata;
maxM[nod][0] = cost;
//
for (int i = 1; i < LOG; i++) {
up[nod][i] = up[up[nod][i-1]][i-1];
maxM[nod][i] = max(maxM[nod][i-1], maxM[up[nod][i-1]][i-1]);
}
//
for (const auto &x : G[nod])
if (x.y != tata)
DFS(x.y, nod, x.c);
}
int Query(int x, int y) {
int sol = 0;
//
if (depth[x] > depth[y])
swap(x, y);
//
for (int i=LOG-1; i>=0; i--)
if (depth[y] - (1 << i) >= depth[x]) {
sol = max(sol, maxM[y][i]);
y = up[y][i];
}
//
if (x == y) return sol;
//
for (int i=LOG-1; i>=0; i--) {
if (up[x][i] != up[y][i]) {
sol = max(sol, maxM[x][i]);
sol = max(sol, maxM[y][i]);
//
x = up[x][i];
y = up[y][i];
}
}
//
sol = max(sol, maxM[x][0]);
sol = max(sol, maxM[y][0]);
//
return sol;
}
int main(){
citire();
Kruskal();
//
depth[0] = -1;
DFS(1, 0, 0);
//
int x, y;
while(K--) {
f >> x >> y;
g << Query(x, y) << '\n';
}
//
f.close();
g.close();
return 0;
}