Pagini recente » Cod sursa (job #84267) | Cod sursa (job #1012162) | Monitorul de evaluare | Cod sursa (job #1678166) | Cod sursa (job #3352987)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
// #define fin cin
// #define fout cout
const int nmax = 15002;
int n,m,k,tabs;
int depth[nmax];
bool viz[nmax];
vector<pair<int,int> > adj[nmax]; // graful il construiesc in apm
struct MuchStr {
int a,b,c;
bool operator < (const MuchStr &other) const {
return c > other.c;
}
};
priority_queue<MuchStr> q;
struct apmStr{
vector<int> sz,fat,costToFat;
apmStr(int nodes)
{
sz.resize(nodes+1); fat.resize(nodes+1); costToFat.resize(nodes+1);
for(int i=1; i<=nodes; i++)
{
sz[i]=1;
fat[i]=i;
costToFat[i] = 0;
}
}
int getFat(int nod)
{
//return fat[nod] = getFat(fat[nod]);
//scot optimizarea asta ca sa pot parcurge
while(nod != fat[nod]) nod = fat[nod];
return nod;
}
void uneste(int a, int b, int c)
{
a = getFat(a);
b = getFat(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a,b);
sz[a] += sz[b];
fat[b] = a;
adj[b].push_back({a,c});
adj[a].push_back({b,c});
costToFat[b] = c;
}
bool sameTr(int a, int b)
{
return getFat(a) == getFat(b);
}
};
void calc_depth(int nod, int h)
{
if (viz[nod]) return;
viz[nod] = 1;
depth[nod] = h;
for (auto pi: adj[nod]) {
calc_depth(pi.first, h + 1);
}
}
int main()
{
fin>>n>>m>>k;
apmStr apm = apmStr(n);
for(int i = 0; i < m; i++) {
int a, b, c;
fin >> a >> b >> c;
q.push({a, b, c});
}
while (!q.empty()) {
auto [a, b, c] = q.top();
q.pop();
if(apm.sameTr(a, b)) continue;
apm.uneste(a, b, c);
}
tabs = apm.getFat(1);
calc_depth(tabs, 0);
for (int i = 0; i < k; i++) {
int a, b;
fin >> a >> b;
int maxCost = 0;
while (depth[a] > depth[b]) {
maxCost = max(maxCost, apm.costToFat[a]);
a = apm.fat[a];
}
while (depth[a] < depth[b]) {
maxCost = max(maxCost, apm.costToFat[b]);
b = apm.fat[b];
}
while (a!=b) {
maxCost = max(maxCost, apm.costToFat[a]);
a = apm.fat[a];
maxCost = max(maxCost, apm.costToFat[b]);
b = apm.fat[b];
}
fout<<maxCost<<"\n";
}
return 0;
}