Submission #51338

# Submission time Handle Problem Language Result Execution time Memory
51338 2018-06-17T13:52:36 Z KieranHorgan Sightseeing (NOI14_sightseeing) C++17
10 / 25
3135 ms 224984 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())

int N, E, Q;

/*-----------------------UNION FIND-----------------------*/
struct UnionFind {
  vector<int> sz, parent;
  int noOfComponents;
  UnionFind(int N_) {
    noOfComponents = N_;
    sz.assign(N_, 1);
    parent.assign(N_, 0);
    for(int i = 0; i < N_; i++) parent[i] = i;
  }
  int findSet(int i) {                            // O(α(N))
    if(i != parent[i])
      return parent[i]=findSet(parent[i]);
    return parent[i];
  }
  int isSameSet(int i,int j){return findSet(i)==findSet(j);}
  void mergeSet(int i, int j) {                   // O(α(N))
    int x = findSet(i);
    int y = findSet(j);
    if(x == y) return;
    if(sz[x] < sz[y]) swap(x, y);
    parent[y] = x;
    sz[x] += sz[y];
    noOfComponents++;
  }
};

/*-----------------MINIMUM SPANNING TREE------------------*/
#define iii pair<int, pair<int, int>>
vector<iii> mst;
vector<iii> EdgeList;
vector<pair<int, int>> AdjList[500005];
void Kruskal() {
  UnionFind UF(N);
  sort(EdgeList.rbegin(), EdgeList.rend());        // O(NlogN)
  for(auto e: EdgeList) {                            // O(N)
    int u = e.second.first;
    int v = e.second.second;
    int w = e.first;
    if(!UF.isSameSet(u,v)) {
      UF.mergeSet(u,v);
      AdjList[u].push_back({w, v});
      AdjList[v].push_back({w, u});
    }
  }
}

int ans[500005];
int vis[500005];
void dfs(int u, int d) {
  vis[u] = 1;
  ans[u] = d;
  for(auto v: AdjList[u])
    if(!vis[v.second])
      dfs(v.second, min(d, v.first));
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);

  cin >> N >> E >> Q;
  for(int i = 0; i < E; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    EdgeList.push_back({w, {u, v}});
  }
  Kruskal();
  dfs(1, 1ll<<29);

  for(int i = 0; i < Q; i++) {
    int x;
    cin >> x;
    cout << ans[x] << endl;
  }
}
# Verdict Execution time Memory Grader output
1 Runtime error 27 ms 24184 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 24596 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 62 ms 31320 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2068 ms 129932 KB Output is correct
2 Correct 3135 ms 224984 KB Output is correct