Submission #124929

#TimeUsernameProblemLanguageResultExecution timeMemory
124929NMAC427Sightseeing (NOI14_sightseeing)C++17
25 / 25
3115 ms187824 KiB
// 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...