Pagini recente » Cod sursa (job #874723) | Cod sursa (job #912011) | Cod sursa (job #1945120) | Cod sursa (job #1121923) | Cod sursa (job #2737683)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
const int nmax = 15005;
int n, m, q, r[nmax], t[nmax], dp[nmax][20][2], lev[nmax];
bool viz[nmax];
vector <pair <int, int> > G[nmax];
struct Muchie{
int x, y, z;
bool operator < (const Muchie &a) const {
return z < a.z;
}
}v[30005];
int getParent(int x){
int aux = x;
while (t[x] != x){
x = t[x];
}
while (t[aux] != aux){
int tata = t[aux];
t[aux] = x;
aux = tata;
}
return x;
}
void Union(int x, int y){
int a = getParent(x);
int b = getParent(y);
if (r[a] < r[b]){
t[a] = b;
}
else if (r[b] < r[a]){
t[b] = a;
}
else{
t[a] = b;
r[b]++;
}
}
void dfs(int nod, int tata){
viz[nod] = true;
for (int i = 1; i < 18; ++i){
int x = dp[nod][i - 1][0];
if (x != -1){
if (dp[x][i - 1][0] != -1){
dp[nod][i][0] = dp[x][i - 1][0];
dp[nod][i][1] = max(dp[nod][i - 1][1], dp[x][i - 1][1]);
}
}
}
for (auto it : G[nod]){
if (it.first == tata){
continue;
}
lev[it.first] = 1 + lev[nod];
dp[it.first][0][0] = nod;
dp[it.first][0][1] = it.second;
dfs(it.first, nod);
}
}
int answer(int x, int y){
if (lev[x] < lev[y]) swap(x, y);
int maxim = 0;
if (lev[x] != lev[y])
{
for (int i = 14; i >= 0; --i)
if (lev[x] - (1 << i) >= lev[y]){
maxim = max(maxim, dp[x][i][1]);
x = dp[x][i][0];
}
}
if (x == y){
return maxim;
}
for (int i = 18; i >= 0; --i){
if (dp[x][i][0] != -1 && dp[y][i][0] != -1 && dp[x][i][0] != dp[y][i][0]){
maxim = max(maxim, dp[y][i][1]);
maxim = max(maxim, dp[x][i][1]);
x = dp[x][i][0];
y = dp[y][i][0];
}
}
maxim = max(maxim, max(dp[x][0][1], dp[y][0][1]));
return maxim;
}
int main(){
fin >> n >> m >> q;
for (int i = 1; i <= n; ++i){
t[i] = i;
}
for (int i = 1; i <= m; ++i){
int x, y, z;
fin >> x >> y >> z;
v[i] = {x, y, z};
}
sort(v + 1, v + m + 1);
for (int i = 1; i <= m; ++i){
if (getParent(v[i].x) != getParent(v[i].y)){
Union(v[i].x, v[i].y);
G[v[i].x].push_back({v[i].y, v[i].z});
G[v[i].y].push_back({v[i].x, v[i].z});
}
}
memset(dp, -1, sizeof dp);
for (int i = 1; i <= n; ++i){
if (viz[i] == false){
dfs(i, 0);
}
}
while (q--){
int x, y;
fin >> x >> y;
fout << answer(x, y) << "\n";
}
return 0;
}