Submission #124929

# Submission time Handle Problem Language Result Execution time Memory
124929 2019-07-04T07:26:33 Z NMAC427 Sightseeing (NOI14_sightseeing) C++17
25 / 25
3115 ms 187824 KB
// 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
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 4728 KB Output is correct
2 Correct 37 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2243 ms 105548 KB Output is correct
2 Correct 3115 ms 187824 KB Output is correct