結果

問題 No.2642 Don't cut line!
ユーザー KudeKude
提出日時 2024-02-19 22:19:00
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 4,000 ms
コード長 6,565 bytes
コンパイル時間 4,014 ms
コンパイル使用メモリ 305,348 KB
実行使用メモリ 28,288 KB
最終ジャッジ日時 2024-09-29 03:34:47
合計ジャッジ時間 6,565 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 93 ms
27,904 KB
testcase_02 AC 87 ms
28,288 KB
testcase_03 AC 82 ms
25,472 KB
testcase_04 AC 77 ms
25,344 KB
testcase_05 AC 81 ms
26,496 KB
testcase_06 AC 39 ms
6,816 KB
testcase_07 AC 40 ms
6,816 KB
testcase_08 AC 39 ms
6,820 KB
testcase_09 AC 38 ms
6,820 KB
testcase_10 AC 38 ms
6,816 KB
testcase_11 AC 38 ms
6,816 KB
testcase_12 AC 39 ms
6,820 KB
testcase_13 AC 37 ms
6,816 KB
testcase_14 AC 38 ms
6,816 KB
testcase_15 AC 38 ms
6,820 KB
testcase_16 AC 59 ms
10,496 KB
testcase_17 AC 64 ms
23,296 KB
testcase_18 AC 69 ms
23,168 KB
testcase_19 AC 51 ms
18,816 KB
testcase_20 AC 37 ms
9,728 KB
testcase_21 AC 37 ms
6,816 KB
testcase_22 AC 41 ms
8,704 KB
testcase_23 AC 82 ms
26,112 KB
testcase_24 AC 40 ms
11,392 KB
testcase_25 AC 39 ms
10,624 KB
testcase_26 AC 39 ms
6,820 KB
testcase_27 AC 54 ms
18,432 KB
testcase_28 AC 77 ms
23,936 KB
testcase_29 AC 40 ms
7,808 KB
testcase_30 AC 40 ms
9,728 KB
testcase_31 AC 62 ms
20,352 KB
testcase_32 AC 38 ms
9,088 KB
testcase_33 AC 1 ms
6,820 KB
testcase_34 AC 2 ms
6,816 KB
testcase_35 AC 2 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

struct HLD {
  int root, n;
  vector<int> sz, parent, depth, idx, ridx, head, inv;

  HLD(const auto& to, int root=0)
      : root(root), n(to.size()),
        sz(n), parent(n), depth(n), idx(n), ridx(n), head(n), inv(n) {
    init_tree_data(to, root, -1, 0);
    int nxt = 0;
    assign_idx(to, root, root, nxt);
  }
  void init_tree_data(const auto& to, int u, int p, int d) {
    parent[u] = p;
    depth[u] = d;
    int s = 1;
    for (auto [v, _]: to[u]) if (v != p) {
      init_tree_data(to, v, u, d+1);
      s += sz[v];
    }
    sz[u] = s;
  }
  void assign_idx(const auto& to, int u, int h, int& nxt, int p=-1) {
    head[u] = h;
    idx[u] = nxt;
    inv[nxt] = u;
    nxt++;
    int heaviest = -1;
    int mxweight = 0;
    for (auto [v, _]: to[u]) if (v != p) {
      if (sz[v] > mxweight) {
        heaviest = v;
        mxweight = sz[v];
      }
    }
    if (heaviest != -1) {
      assign_idx(to, heaviest, h, nxt, u);
      for (auto [v, _]: to[u]) if (v != p && v != heaviest) {
        assign_idx(to, v, v, nxt, u);
      }
    }
    ridx[u] = nxt;
  }

  int lca(int u, int v) {
    while (head[u] != head[v]) {
      if (depth[head[u]] > depth[head[v]]) {
        u = parent[head[u]];
      } else {
        v = parent[head[v]];
      }
    }
    return depth[u] < depth[v] ? u : v;
  }
  // returns reference to tuple of (path fragments from x upto lca (excluding lca), those from y, lca)
  // storage of retval is reused to avoid creating short vectors on each query
  tuple<vector<pair<int, int>>, vector<pair<int, int>>, int> paths_res;
  auto& paths(int x, int y) {
    auto& [x_paths, y_paths, lca] = paths_res;
    x_paths.clear();
    y_paths.clear();
    while (head[x] != head[y]) {
      int hx = head[x], hy = head[y];
      if (depth[hx] > depth[hy]) {
        x_paths.emplace_back(x, hx); x = parent[hx];
      } else {
        y_paths.emplace_back(y, hy); y = parent[hy];
      }
    }
    if (depth[x] > depth[y]) {
      x_paths.emplace_back(x, inv[idx[y] + 1]); x = y;
    } else if (depth[x] < depth[y]) {
      y_paths.emplace_back(y, inv[idx[x] + 1]); y = x;
    }
    lca = x;
    return paths_res;
  }
  int dist(int u, int v) {
    int w = lca(u, v);
    return depth[u] + depth[v] - 2 * depth[w];
  }
  template <class F> int max_ancestor(int v, F f) {
    if (!f(v)) return -1;
    int hv = head[v];
    int p = parent[hv];
    while (p != -1 && f(p)) {
      v = p;
      hv = head[v];
      p = parent[hv];
    }
    int il = idx[hv] - 1, ir = idx[v];
    while (ir - il > 1) {
      int ic = (il + ir) / 2;
      (f(inv[ic]) ? ir : il) = ic;
    }
    return inv[ir];
  }
  int ascend(int v, int k) {
    assert(depth[v] >= k);
    int td = depth[v] - k;
    int hv = head[v];
    while (depth[hv] > td) {
      v = parent[hv];
      hv = head[v];
    }
    int rest = depth[v] - td;
    return inv[idx[v] - rest];
  }
  int move_to(int u, int v, int by) {
    int l = lca(u, v);
    int du = depth[u] - depth[l];
    int dv = depth[v] - depth[l];
    assert(by <= du + dv);
    if (by <= du) return ascend(u, by);
    else return ascend(v, du + dv - by);
  }
};


template <class S, S (*op)(S, S)>
struct DisjointSparseTable {
  const int n;
  const vector<unsigned char> msb;
  const vector<vector<S>> d;
  DisjointSparseTable(vector<S> a) : n(a.size()), msb(build_msb_table(n)), d(build_table(move(a))) {}

  vector<unsigned char> build_msb_table(int n) {
    if (n <= 1) return {};
    unsigned char k_max = 32 - __builtin_clz(n - 1);
    vector<unsigned char> res(1 << k_max);
    unsigned char* p = res.data();
    for (unsigned char k = 0; k < k_max; k++) {
      memset(p + (1U << k), k, 1U << k);
    }
    return res;
  }
  vector<vector<S>> build_table(vector<S> a) {
    const int n = a.size();
    vector<vector<S>> res(1);
    if (n >= 2) {
      const int i_max = n - 1, k_max = 32 - __builtin_clz(i_max);
      res.resize(k_max);
      for (int k = 1; k < k_max; k++) {
        vector<S> t(i_max >> k & 1 ? n : i_max & ~0U << k);
        for (int c = 1 << k; c < n; c += 1 << (k + 1)) {
          int l = c - (1 << k);
          int r = min(n, c + (1 << k));
          t[c - 1] = a[c - 1];
          for (int i = c - 2; i >= l; i--) t[i] = op(a[i], t[i + 1]);
          t[c] = a[c];
          for (int i = c + 1; i < r; i++) t[i] = op(t[i - 1], a[i]);
        }
        res[k] = move(t);
      }
    }
    res[0] = move(a);
    return res;
  }

  S query(int l, int r) { return query_closed(l, r - 1); }
  S query_closed(int l, int r) {
    assert(0 <= l && l <= r && r < n);
    if (l == r) return d[0][l];
    auto k = msb[l ^ r];
    return op(d[k][l], d[k][r]);
  }
};

int op(int x, int y) { return max(x, y); }

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  ll c;
  cin >> n >> m >> c;
  struct E {
    int u, v, w, p;
  };
  vector<E> es(m);
  for (auto& [u, v, w, p] : es) {
    cin >> u >> v >> w >> p;
    u--, v--;
  }
  ranges::sort(es, {}, &E::w);
  vector<vector<P>> to(n);
  dsu uf(n);
  int ans = 0;
  ll min_cost = 0;
  for (auto [u, v, w, p] : es) {
    if (uf.same(u, v)) continue;
    to[u].emplace_back(v, w);
    to[v].emplace_back(u, w);
    chmax(ans, p);
    min_cost += w;
    uf.merge(u, v);
  }
  if (min_cost > c) {
    cout << -1 << '\n';
    return 0;
  }
  HLD hld(to);
  VI init(n);
  rep(u, n) if (u) {
    for (auto [v, w] : to[u]) if (v == hld.parent[u]) {
      init[hld.idx[u]] = w;
      break;
    }
  }
  DisjointSparseTable<int, op> dst(init);
  for (auto [u, v, w, p] : es) if (p > ans) {
    auto& [p1, p2, lca] = hld.paths(u, v);
    int cmx = 0;
    rep(_, 2) {
      for (auto [x, y] : p1) {
        int ix = hld.idx[x], iy = hld.idx[y];
        chmax(cmx, dst.query_closed(iy, ix));
      }
      swap(p1, p2);
    }
    if (min_cost - cmx + w <= c) ans = p;
  }
  cout << ans << '\n';
}
0