답안 #124615

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124615 2019-07-03T15:07:01 Z streifi 관광 (NOI14_sightseeing) C++14
15 / 25
3500 ms 95864 KB
#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

struct adj_edge {
	int v, w;
};

struct list_edge {
	int u, v, w;
	bool operator<(const list_edge &other) const {
		return w < other.w;
	}
};

int V, E, Q;
vector< vector<adj_edge> > adj;
vector<list_edge> edge_list;
vector<int> par, dist;

int find(int u) {
	if (par[u] == u)
		return u;
	return par[u] = find(par[u]);
}

void merge(int u, int v) {
	int ru = find(u), rv = find(v);
	if (ru == rv)
		return;
	if (rand()%2)
		swap(ru, rv);
	par[ru] = rv;
}

void kruskall() {
	par.resize(V);
	for (int i = 0; i < V; ++i)
		par[i] = i;
	sort(edge_list.rbegin(), edge_list.rend());
	for (list_edge e: edge_list) {
		if (find(e.u) == find(e.v))
			continue;
		merge(e.u, e.v);
		adj[e.u].push_back({e.v, e.w});
		adj[e.v].push_back({e.u, e.w});
	}
}

void dfs(int cur, int curmin) {
	if (dist[cur] != -1)
		return;
	dist[cur] = curmin;
	for (adj_edge e: adj[cur]) {
		dfs(e.v, min(curmin, e.w));
	}
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> V >> E >> Q;
	adj.resize(V);
	edge_list.resize(E);
	for (int e = 0; e < E; ++e) {
		int u, v, q;
		cin >> u >> v >> q;
		edge_list[e] = list_edge{u-1, v-1, q};
	}
	kruskall();
	dist.assign(V, -1);
	dfs(0, INF);
	for (int q = 0; q < Q; ++q) {
		int X;
		cin >> X;
		cout << dist[X-1] << endl;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 3408 KB Output is correct
2 Correct 48 ms 3192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2639 ms 62128 KB Output is correct
2 Execution timed out 3563 ms 95864 KB Time limit exceeded