結果

問題 No.2642 Don't cut line!
ユーザー MisukiMisuki
提出日時 2024-02-19 22:24:43
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,017 bytes
コンパイル時間 3,838 ms
コンパイル使用メモリ 222,980 KB
実行使用メモリ 10,188 KB
最終ジャッジ日時 2024-02-19 22:24:51
合計ジャッジ時間 6,456 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
9,944 KB
testcase_01 AC 92 ms
9,944 KB
testcase_02 AC 90 ms
9,944 KB
testcase_03 AC 88 ms
9,944 KB
testcase_04 AC 89 ms
9,944 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 46 ms
9,944 KB
testcase_16 AC 31 ms
9,196 KB
testcase_17 AC 34 ms
9,604 KB
testcase_18 AC 25 ms
8,264 KB
testcase_19 AC 89 ms
10,188 KB
testcase_20 AC 37 ms
7,168 KB
testcase_21 AC 34 ms
8,520 KB
testcase_22 AC 47 ms
9,816 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 31 ms
7,424 KB
testcase_27 AC 46 ms
9,448 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 36 ms
8,684 KB
testcase_31 WA -
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 1 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>

#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

#define clock chrono::steady_clock::now().time_since_epoch().count()

#ifdef DEBUG
#define dbg(x) cout << (#x) << " = " << x << '\n'
#else
#define dbg(x)
#endif

namespace R = std::ranges;
namespace V = std::views;

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
//#define double ldb

template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(const T &X : arr)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(const T &X : vec)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
  for(const T &x : s)
    os << x << ' ';
  return os;
}

struct DSU {
  vector<int> bos, sz;
  int size;

  DSU(int _size) : bos(_size), sz(_size, 1), size(_size) {
    iota(bos.begin(), bos.end(), 0);
  }

  int query(int v) {
    if (bos[v] == v)
      return v;
    else
      return bos[v] = query(bos[v]);
  }

  bool merge(int v1, int v2) {
    int b1 = query(v1), b2 = query(v2);

    if (b1 == b2)
      return false;

    if (sz[b1] > sz[b2])
      swap(b1, b2);
    bos[b1] = b2, sz[b2] += sz[b1];

    return true;
  }
};

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int n, k, c; cin >> n >> k >> c;
  vector<array<int, 4>> e(k);
  for(auto &[u, v, w, p] : e) {
    cin >> u >> v >> w >> p;
    u--, v--;
  }

  vector<int> ord(k);
  iota(ord.begin(), ord.end(), 0);
  sort(ord.begin(), ord.end(), [&](int i, int j) { return e[i][2] < e[j][2]; });

  vector<bool> inT(k, false);
  vector<int> teid;
  int cost = 0, price = 0;
  {
    DSU dsu(n);
    for(int i = 0; auto [u, v, w, p] : e) {
      if (dsu.merge(u, v)) {
        inT[i] = true, cost += w, price = max(price, p);
        teid.emplace_back(i);
      }
      i++;
    }
  }

  if (cost > c) {
    cout << -1 << '\n';
    return 0;
  }

  vector<array<int, 3>> bs;
  for(int i = 0; i < k; i++)
    if (!inT[i])
      bs.push_back({0, n - 2, i});

  while(!bs.empty()) {
    DSU dsu(n);
    sort(bs.begin(), bs.end(), [](auto x, auto y) { return (x[0] + x[1]) / 2 < (y[0] + y[1]) / 2; });
    vector<array<int, 3>> nxt;
    int ptr = 0;
    for(auto [l, r, i] : bs) {
      if (l == r) {
        if (int tmp = cost - e[teid[l]][2] + e[i][2]; tmp <= c)
          price = max(price, e[i][3]);
        continue;
      }
      int mid = (l + r) / 2;
      while(ptr < mid) {
        int k = teid[ptr++];
        dsu.merge(e[k][0], e[k][1]);
      }
      if (dsu.query(e[i][0]) == dsu.query(e[i][1]))
        nxt.push_back({l, mid, i});
      else
        nxt.push_back({mid + 1, r, i});
    }
    bs.swap(nxt);
  }

  cout << price << '\n';

  return 0;
}
0