This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/NOI14_sightseeing
#include <bits/stdc++.h>
#define int int_fast32_t
#define coutV(x) for (const auto& e: x) {cerr << e << " ";} cerr << "\n"
using namespace std;
class UnionFind {
vector<int> p;
public:
UnionFind(size_t size) {
p.resize(size);
iota(p.begin(), p.end(), 0);
}
int parent(int v) {
return p[v] = (p[v] == v ? v : parent(p[v]));
}
void unite(int a, int b) {
if (rand() % 2) swap(a, b);
p[parent(a)] = parent(b);
}
};
int V, E, Q;
signed main() {
#ifndef __APPLE__
cin.tie(0);
ios_base::sync_with_stdio(0);
#endif
cin >> V >> E >> Q;
vector<pair<int, pair<int, int>>> edges;
edges.resize(E);
for (int i = 0; i < E; i++) {
int v1, v2, q;
cin >> v1 >> v2 >> q;
edges[i] = {q, {v1 - 1, v2 - 1}};
}
// MST
sort(edges.rbegin(), edges.rend());
UnionFind uf (V);
vector<vector<pair<int, int>>> adj;
adj.resize(V);
for (const auto& e: edges) {
if (uf.parent(e.second.first) == uf.parent(e.second.second)) continue;
uf.unite(e.second.first, e.second.second);
adj[e.second.first].emplace_back(e.second.second, e.first);
adj[e.second.second].emplace_back(e.second.first, e.first);
}
// DFS
vector<int> visited (V, -1);
stack<pair<int, int>> s;
s.push({INT32_MAX, 0});
while (!s.empty()) {
auto top = s.top();
s.pop();
int v = top.second;
int pathQ = top.first;
if (visited[v] != -1) {
continue;
}
visited[v] = pathQ;
for (const auto& child: adj[v]) {
s.push({min(pathQ, child.second), child.first});
}
}
for (int i = 0; i < Q; i++) {
int x;
cin >> x;
cout << visited[x - 1] << "\n";
}
}
# | 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... |