Pagini recente » Cod sursa (job #2723766) | Cod sursa (job #3218890) | Cod sursa (job #3222521) | Cod sursa (job #1283403) | Cod sursa (job #3291326)
// 100 Puncte
#include <bits/stdc++.h>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
vector<tuple<int, int, int>> v; // (cost, u, v)
int root[15005];
int height[15005];
int tata[20][15005];
int medg[20][15005];
int lg2[15005];
vector<pair<int, int>> g[15005]; // now stores (neighbor, edge_cost)
bool viz[15005];
int depth[15005];
int find_root(int x) {
if (root[x] != x)
root[x] = find_root(root[x]);
return root[x];
}
void find_depth(int nod, int par, int cost) {
viz[nod] = 1;
tata[0][nod] = par;
medg[0][nod] = cost;
for (auto [vecin, c] : g[nod]) {
if (!viz[vecin]) {
depth[vecin] = depth[nod] + 1;
find_depth(vecin, nod, c);
}
}
}
int main() {
fin.tie(0); fin.sync_with_stdio(false);
int n, m, k; fin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
int x, y, c;
fin >> x >> y >> c;
v.emplace_back(c, x, y);
}
for (int i = 1; i <= n; i++) root[i] = i;
sort(v.begin(), v.end());
for (auto [c, x, y] : v) {
int root_x = find_root(x);
int root_y = find_root(y);
if (root_x != root_y) {
if (height[root_x] < height[root_y]) {
swap(root_x, root_y);
}
root[root_y] = root_x;
if (height[root_x] == height[root_y]) height[root_x]++;
g[x].emplace_back(y, c);
g[y].emplace_back(x, c);
}
}
find_depth(1, 0, 0);
for (int i = 2; i <= n; i++) lg2[i] = lg2[i / 2] + 1;
for (int i = 1; (1 << i) <= n; i++) {
for (int j = 1; j <= n; j++) {
tata[i][j] = tata[i - 1][tata[i - 1][j]];
medg[i][j] = max(medg[i - 1][j], medg[i - 1][tata[i - 1][j]]);
}
}
while (k--) {
int x, y;
fin >> x >> y;
if (x == y) {
fout << 0 << '\n';
continue;
}
int dx = depth[x], dy = depth[y];
if (dx < dy) {
swap(x, y);
swap(dx, dy);
}
int diff = dx - dy;
int maxi_muchie = 0;
while (diff) {
int strat = lg2[diff];
maxi_muchie = max(maxi_muchie, medg[strat][x]);
x = tata[strat][x];
diff = depth[x] - dy;
}
if (x == y) {
fout << maxi_muchie << '\n';
continue;
}
for (int i = lg2[n]; i >= 0; i--) {
if (tata[i][x] != 0 && tata[i][x] != tata[i][y]) {
maxi_muchie = max({maxi_muchie, medg[i][x], medg[i][y]});
x = tata[i][x];
y = tata[i][y];
}
}
maxi_muchie = max({maxi_muchie, medg[0][x], medg[0][y]});
fout << maxi_muchie << '\n';
}
return 0;
}