Pagini recente » Cod sursa (job #718750) | Cod sursa (job #347277) | Cod sursa (job #327662) | Cod sursa (job #1081670) | Cod sursa (job #3154301)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
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)
{
int fa = getFat(a);
int fb = getFat(b);
if(fa == fb) return;
if(sz[fa] < sz[fb]) swap(fa,fb);
//unesc fb la fa
sz[fa] += sz[fb];
fat[fb] = fa;
adj[fb].push_back({fa,c});
adj[fa].push_back({fb,c});
costToFat[fb] = c;
}
bool sameTr(int a, int b)
{
return getFat(a) == getFat(b);
}
};
void assignDepth(int nod, int h)
{
if(viz[nod]) return;
viz[nod] = 1;
depth[nod] = h;
for(auto pi: adj[nod])
{
assignDepth(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())
{
int a=q.top().a,b=q.top().b,c=q.top().c;
q.pop();
if(apm.sameTr(a,b)) continue;
apm.uneste(a,b,c);
}
tabs = apm.getFat(1);
assignDepth(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;
}