답안 #124594

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124594 2019-07-03T14:46:23 Z MoNsTeR_CuBe 관광 (NOI14_sightseeing) C++17
15 / 25
3500 ms 58404 KB
#include <bits/stdc++.h>
using namespace std;

const int INF = numeric_limits<int>::max();

vector< int > dist;

vector< int > U;

int getParents(int a){
	if(U[a] == a) return a;
	else return getParents(U[a]);
}

void Union(int a, int b){
	U[getParents(a)] = getParents(b);
}

vector< vector< pair<int, int> > > Graph;

vector< bool > visited;

void dfs(int node, int cost){
	dist[node] = cost;
	visited[node] = true;
	for(pair<int, int> p : Graph[node]){
		if(visited[p.first]) continue;
		dfs(p.first, min(cost, p.second));
	}
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n, m, q;
	cin >> n >> m >> q;
	
	Graph.resize(n+1);
	
	vector< tuple<int, int, int> > edges;
	
	for(int i = 0; i < m; i++){
		
		int a, b, c;
		cin >> a >> b >> c;
		
		edges.push_back(make_tuple(c,a,b));
	}
	
	dist.resize(n+1);
	U.resize(n+1);
	
	for(int i = 0; i <= n; i++){
		U[i] = i;
	}
	
	sort(edges.begin(), edges.end());
	
	reverse(edges.begin(), edges.end());
	
	for(int i = 0; i < m; i++){
		int a = get<1>(edges[i]);
		int b = get<2>(edges[i]);
		
		int c = get<0>(edges[i]);
		
		if(getParents(a) == getParents(b)){
			continue;
		}
		Union(a,b);
		
		Graph[a].push_back(make_pair(b,c));
		Graph[b].push_back(make_pair(a,c));
	}
	visited.assign(n+1, false);
	dfs(1,INF);
	
	for(int i = 0; i < q; i++){
		int a;
		cin >> a;
		cout << dist[a] << 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 4 ms 504 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 797 ms 3484 KB Output is correct
2 Correct 429 ms 3184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3539 ms 58404 KB Time limit exceeded
2 Halted 0 ms 0 KB -