답안 #218228

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
218228 2020-04-01T15:03:09 Z quocnguyen1012 관광 (NOI14_sightseeing) C++14
25 / 25
2658 ms 199032 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 5e5 + 5;

int N, M, Q;
int lab[maxn];
vector<ii> adj[maxn];
int path[maxn];

void init(int n)
{
  fill_n(&lab[0], n + 1, -1);
}

int finds(int u)
{
  if(lab[u] < 0) return u;
  return lab[u] = finds(lab[u]);
}

bool merges(int u, int v)
{
  u = finds(u); v = finds(v);
  if(u == v) return false;
  if(lab[v] < lab[u]) swap(u, v);
  lab[u] += lab[v];
  lab[v] = u;
  return true;
}

void dfs(int u, int p = -1)
{
  for(auto & v : adj[u]) if(v.fi != p){
    path[v.fi] = min(v.se, path[u]);
    dfs(v.fi, u);
  }
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N >> M >> Q;
  init(N);
  vector<pair<int, ii>> edge(M);
  for(auto & it : edge){
    cin >> it.se.fi >> it.se.se >> it.fi;
  }
  sort(edge.rbegin(), edge.rend());
  for(auto & it : edge){
    if(merges(it.se.fi, it.se.se)){
      //cerr << it.se.fi << ' ' << it.se.se << ' ' << it.fi << '\n';
      adj[it.se.fi].eb(it.se.se, it.fi);
      adj[it.se.se].eb(it.se.fi, it.fi);
    }
  }
  path[1] = 1e9;
  dfs(1);
  while(Q--){
    int x; cin >> x;
    cout << path[x] << '\n';
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12160 KB Output is correct
2 Correct 11 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 15224 KB Output is correct
2 Correct 35 ms 14848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1737 ms 123680 KB Output is correct
2 Correct 2658 ms 199032 KB Output is correct