Pagini recente » Cod sursa (job #600175) | Solutii Autumn Warmup, Runda 3 | Cod sursa (job #1376637) | Cod sursa (job #2436416) | Cod sursa (job #2500586)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("radiatie.in");
ofstream out("radiatie.out");
int t[200005];
int h[200005];
int findSet(int x)
{
while (x != t[x])
x = t[x];
return x;
}
void uniteSet(int x, int y)
{
x = findSet(x);
y = findSet(y);
if (x == y)
return;
if (h[x] == h[y])
{
t[y] = x;
h[x]++;
}
else if (h[x] < h[y])
t[x] = y;
else
t[y] = x;
}
struct edge {
int x, y, c;
};
vector<edge> G;
vector<pair<int, int>> apmEdges[15005];
int n, m, q;
int dist[15005];
int main()
{
in >> n >> m >> q;
for (int i = 1; i <= n; i++)
{
t[i] = i;
h[i] = 1;
}
for (int i = 1; i <= m; i++)
{
int x, y, c;
in >> x >> y >> c;
G.push_back({ x, y, c });
}
sort(G.begin(), G.end(), [](const edge& l, const edge& r) { return l.c < r.c; });
for (const edge& e : G)
{
if (findSet(e.x) != findSet(e.y))
{
uniteSet(e.x, e.y);
apmEdges[e.x].push_back({ e.y,e.c });
apmEdges[e.y].push_back({ e.x, e.c });
}
}
while (q--)
{
int x, y;
in >> x >> y;
queue<int> q;
q.push(x);
dist[x] = 0;
while (!q.empty())
{
int a = q.front();
q.pop();
if (a == y)
break;
for (pair<int, int> b : apmEdges[a])
{
dist[b.first] = max(dist[a], b.second);
q.push(b.first);
}
}
out << dist[y] << '\n';
}
}