Pagini recente » Cod sursa (job #3281283) | Cod sursa (job #2923738) | Cod sursa (job #3032212) | Cod sursa (job #2545045) | Cod sursa (job #3291324)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
vector<pair<int, pair<int, int>>> v;
int root[15005];
int height[15005];
int tata[20][15005];
int medg[20][15005];
int lg2[15005];
vector<int> g[15005];
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 (int vecin : g[nod]) {
if (!viz[vecin]) {
depth[vecin] = depth[nod] + 1;
find_depth(vecin, nod, cost); // cost will be set properly in Kruskal below
}
}
}
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.push_back({c, {x, y}});
}
for (int i = 1; i <= n; i++) root[i] = i;
sort(v.begin(), v.end());
for (int i = 0; i < (int)v.size(); i++) {
int c = v[i].first;
int x = v[i].second.first;
int y = v[i].second.second;
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].push_back(y);
g[y].push_back(x);
}
}
// Run DFS to assign depth, parents and edge weights
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;
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;
}