답안 #38882

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
38882 2018-01-07T14:19:33 Z cheater2k 관광 (NOI14_sightseeing) C++14
15 / 25
53 ms 22628 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 500005;

int n, m, q;
int dsu[N];
vector< pair<int,int> > edges[100005];
vector< pair<int,int> > G[N];
int mn[N];

int anc(int p) { return p == dsu[p] ? p : dsu[p] = anc(dsu[p]); }
void join(int p, int q) { p = anc(p); q = anc(q); dsu[p] = q; }

inline void dfs(int u, int p) {
	for (int i = 0; i < G[u].size(); ++i) {
		int v = G[u][i].second, w = G[u][i].first; if (v == p) continue;
		mn[v] = min(mn[u], w);
		dfs(v, u);
	}
}

int main() {
	scanf("%d %d %d", &n, &m, &q);
	for (int i = 0; i < m; ++i) {
		int u, v, w; scanf("%d %d %d", &u, &v, &w);
		edges[w].push_back(make_pair(u, v)); // w <= 1e5
	}
	// build maximum spanning tree
	for (int i = 1; i <= n; ++i) dsu[i] = i;
	for (int w = 100000; w >= 0; --w) if (edges[w].size()) {
		for (int i = 0; i < edges[w].size(); ++i) {
			int u = edges[w][i].first, v = edges[w][i].second;
			if (anc(u) != anc(v)) {
				join(u, v);
				G[u].push_back(make_pair(w, v)); 
				G[v].push_back(make_pair(w, u));
			}
		}
	}

	mn[1] = 1e9;
	dfs(1, 0);
	
	// answer queries
	while(q--) {
		int u; scanf("%d", &u);
		printf("%d\n", mn[u]);
	}
}

Compilation message

sightseeing.cpp: In function 'void dfs(int, int)':
sightseeing.cpp:16:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < G[u].size(); ++i) {
                    ^
sightseeing.cpp: In function 'int main()':
sightseeing.cpp:32:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < edges[w].size(); ++i) {
                     ^
sightseeing.cpp:24:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &m, &q);
                               ^
sightseeing.cpp:26:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v, w; scanf("%d %d %d", &u, &v, &w);
                                             ^
sightseeing.cpp:47:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u; scanf("%d", &u);
                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19988 KB Output is correct
2 Correct 0 ms 19988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 20120 KB Output is correct
2 Correct 6 ms 19988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 22628 KB Output is correct
2 Correct 53 ms 22232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 0 KB -1: Interrupted system call
2 Halted 0 ms 0 KB -