답안 #1038793

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038793 2024-07-30T07:44:05 Z NeroZein Reconstruction Project (JOI22_reconstruction) C++17
0 / 100
1 ms 348 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif

const int N = 502;

int n, m;
int p[N], sz[N];
map<int, vector<int>> mp;
map<pair<int, int>, long long> ps;

int get(int v) {
  return p[v] = (p[v] == v ? v : get(p[v]));
}

void unite(int u, int v) {
  u = get(u); v = get(v);
  if (sz[u] > sz[v]) {
    swap(u, v); 
  }
  sz[v] += sz[u];
  p[u] = v; 
}

void simulate(int x, vector<array<int, 3>> e) {
  if (mp.count(x)) {
    return; 
  }
  sort(e.begin(), e.end(), [&](array<int, 3>& i, array<int, 3>& j) {
    return abs(i[2] - x) < abs(j[2] - x);
  });
  for (int i = 1; i <= n; ++i) {
    p[i] = i;
    sz[i] = 1; 
  }
  long long res = 0; 
  for (int i = 0; i < m; ++i) {
    auto [u, v, w] = e[i];
    if (get(u) != get(v)) {
      unite(u, v);
      res += (long long) abs(w - x); 
      mp[x].push_back(w);
    }
  }
  sort(mp[x].begin(), mp[x].end()); 
  assert((int) mp[x].size() == n - 1); 
  long long sum = 0; 
  for (int i = 0; i < n - 1; ++i) {
    sum += mp[x][i];
    ps[{x, i}] = sum; 
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  vector<array<int, 3>> e(m); 
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    e[i] = {u, v, w};
  }
  sort(e.begin(), e.end(), [&](array<int, 3>& i, array<int, 3>& j) {
    return i[2] < j[2];
  });
  simulate(0, e);
  for (int i = 1; i < m; ++i) {
    simulate((e[i - 1][2] + e[i][2]) / 2, e); 
  }
  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    auto [rx, vec] = *prev(mp.upper_bound(x)); 
    auto ind = upper_bound(vec.begin(), vec.end(), x) - vec.begin(); 
    long long ans = 0; 
    long long pref = 0;
    if (ind > 0) {
      pref = ps[{rx, ind - 1}];
      ans += (long long) (ind) * x - pref;
    }
    if (ind < n - 1) {
      long long suf = ps[{rx, n - 2}] - pref;
      ans += suf - (long long) (n - ind - 1) * x; 
    }
    cout << ans << '\n'; 
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -