Submission #1352062

#TimeUsernameProblemLanguageResultExecution timeMemory
1352062avighnaReconstruction Project (JOI22_reconstruction)C++20
7 / 100
5095 ms5400 KiB
#include <bits/stdc++.h>

using namespace std;

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

class dsu {
  int n;
  vector<int> par;

public:
  dsu(int n) : n(n), par(n) {
    iota(par.begin(), par.end(), 0);
  }

  int root(int u) {
    return u == par[u] ? u : par[u] = root(par[u]);
  }

  void merge(int u, int v) {
    u = root(u), v = root(v);
    if (u != v) {
      par[v] = u;
    }
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<edge> a(m);
  for (auto &[u, v, w] : a) {
    cin >> u >> v >> w;
  }

  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    dsu dsu(n + 1);
    vector<edge> edges = a;
    for (auto &[u, v, w] : edges) {
      w = abs(x - w);
    }
    sort(edges.begin(), edges.end());
    int64_t ans = 0;
    for (auto &[u, v, w] : edges) {
      if (dsu.root(u) != dsu.root(v)) {
        ans += w;
        dsu.merge(u, v);
      }
    }
    cout << ans << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...