Pagini recente » Cod sursa (job #2496562) | Cod sursa (job #2514505) | Cod sursa (job #2259897) | Cod sursa (job #1078706) | Cod sursa (job #3268671)
#include <bits/stdc++.h>
using namespace std;
vector<int> tati;
vector<int> heig;
vector<set<int>> q;
vector<int> ras;
void init(int n)
{
tati.push_back(0);
heig.push_back(0);
for(int i = 1; i <= n; i++)
{
tati.push_back(i);
heig.push_back(1);
}
}
int query(int node)
{
int tn = node;
while(tn != tati[tn])
tn = tati[tn];
int tnn = node;
while(tnn != tati[tnn])
{
int ax = tnn;
tnn = tati[tnn];
tati[ax] = tn;
}
return tn;
}
void join(int node1, int node2, int cost)
{
node1 = query(node1);
node2 = query(node2);
if(q[node1].size() > q[node2].size())
{
swap(node1, node2);
}
for(auto it = q[node1].begin(); it != q[node1].end(); it++)
{
if(q[node2].find((*it)) != q[node2].end())
{
ras[(*it)] = cost;
q[node2].erase((*it));
}
else
{
q[node2].emplace((*it));
}
}
q[node1].clear();
if(heig[node1] < heig[node2])
{
tati[node1] = node2;
}
else if(heig[node1] == heig[node2])
{
tati[node2] = node1;
heig[node1]++;
swap(q[node1], q[node2]);
}
else
{
tati[node2] = node1;
swap(q[node1], q[node2]);
}
}
int main()
{
freopen("radiatie.in", "r", stdin);
freopen("radiatie.out", "w", stdout);
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, pair<int, int>>> v;
for(int i = 0; i < m; i++)
{
int x, y, c;
cin >> x >> y >> c;
v.push_back({c, {x, y}});
}
sort(v.begin(), v.end());
init(n);
q.resize(n+1);
ras.resize(k);
for(int i = 0; i < k; i++)
{
int x, y;
cin >> x >> y;
q[x].emplace(i);
q[y].emplace(i);
}
for(auto it = v.begin(); it != v.end(); it++)
{
if(query((*it).second.first) != query((*it).second.second))
{
join((*it).second.first, (*it).second.second, (*it).first);
}
}
for(auto it = ras.begin(); it != ras.end(); it++)
{
cout << (*it) << "\n";
}
}