# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
51343 | Kubalionzzale | Sightseeing (NOI14_sightseeing) | C++14 | 3191 ms | 102580 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <functional>
std::vector< std::vector< std::pair<int, int> > > g(500010);
int ans[500010] = { 0 }, sums[500010] = { 0 }, roots[500010] = { 0 };
std::pair<int, std::pair<int, int> > edge[5000010];
int n, m, q;
void initialize(int n)
{
for (int i = 0; i < n; ++i)
{
roots[i] = i;
sums[i] = 1;
}
}
int root(int i)
{
while (roots[i] != i)
{
roots[i] = roots[roots[i]];
i = roots[i];
}
return i;
}
void makeUnion(int a, int b)
{
a = root(a);
b = root(b);
if (sums[a] >= sums[b])
{
sums[a] += sums[b];
roots[b] = roots[a];
}
else
{
sums[b] += sums[a];
roots[a] = roots[b];
}
}
void dfs(int index, int parent, int val)
{
ans[index] = val;
for (int i = 0; i < g[index].size(); ++i)
{
int next = g[index][i].second;
int nextVal = g[index][i].first;
if (next != parent)
{
dfs(next, index, std::min(nextVal, val));
}
}
}
int main()
{
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
scanf("%d%d%d", &n, &m, &q);
// std::cin >> n >> m >> q;
int x, y, quality;
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d", &x, &y, &quality);
// std::cin >> x >> y >> quality;
--x,
--y;
edge[i] = std::make_pair(quality, std::make_pair(x, y));
}
std::sort(edge, edge + m);
int node[500010];
for (int i = 0; i < q; ++i)
{
scanf("%d", &node[i]);
// std::cin >> node[i];
}
bool in[500010] = { 0 };
in[0] = true;
initialize(n);
for (int i = m - 1; i >= 0; --i)
{
x = edge[i].second.first;
y = edge[i].second.second;
int val = edge[i].first;
if (root(x) != root(y))
{
makeUnion(root(x), root(y));
g[x].push_back(std::make_pair(val, y));
g[y].push_back(std::make_pair(val, x));
}
}
dfs(0, -1, 1e6);
for (int i = 0; i < q; ++i)
{
printf("%d\n", ans[node[i] - 1]);
// std::cout << ans[node[i] - 1] << "\n";
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |