結果

問題 No.2642 Don't cut line!
ユーザー SSRSSSRS
提出日時 2024-02-19 21:58:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 4,000 ms
コード長 2,751 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 228,244 KB
実行使用メモリ 25,580 KB
最終ジャッジ日時 2024-02-20 12:46:41
合計ジャッジ時間 8,618 ms
ジャッジサーバーID
(参考情報)
judge13 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 181 ms
25,580 KB
testcase_02 AC 176 ms
25,580 KB
testcase_03 AC 178 ms
25,580 KB
testcase_04 AC 183 ms
25,580 KB
testcase_05 AC 185 ms
25,580 KB
testcase_06 AC 117 ms
6,676 KB
testcase_07 AC 120 ms
6,676 KB
testcase_08 AC 118 ms
6,676 KB
testcase_09 AC 120 ms
6,676 KB
testcase_10 AC 122 ms
6,676 KB
testcase_11 AC 118 ms
6,676 KB
testcase_12 AC 122 ms
6,676 KB
testcase_13 AC 117 ms
6,676 KB
testcase_14 AC 119 ms
6,676 KB
testcase_15 AC 123 ms
6,676 KB
testcase_16 AC 145 ms
10,752 KB
testcase_17 AC 135 ms
22,636 KB
testcase_18 AC 145 ms
24,252 KB
testcase_19 AC 103 ms
18,868 KB
testcase_20 AC 104 ms
10,496 KB
testcase_21 AC 107 ms
6,676 KB
testcase_22 AC 106 ms
8,832 KB
testcase_23 AC 183 ms
25,340 KB
testcase_24 AC 90 ms
14,060 KB
testcase_25 AC 99 ms
12,928 KB
testcase_26 AC 124 ms
7,808 KB
testcase_27 AC 111 ms
17,704 KB
testcase_28 AC 160 ms
23,680 KB
testcase_29 AC 118 ms
9,216 KB
testcase_30 AC 105 ms
11,776 KB
testcase_31 AC 139 ms
20,592 KB
testcase_32 AC 111 ms
10,880 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int LOG = 17;
struct unionfind{
  vector<int> p;
  unionfind(int N): p(N, -1){
  }
  int root(int x){
    if (p[x] == -1){
      return x;
    } else {
      p[x] = root(p[x]);
      return p[x];
    }
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if (x != y){
      p[x] = y;
    }
  }
};
int main(){
  int N, K;
  long long C;
  cin >> N >> K >> C;
  vector<tuple<int, int, int, int>> E(K);
  for (int i = 0; i < K; i++){
    int u, v, w, p;
    cin >> u >> v >> w >> p;
    u--;
    v--;
    E[i] = make_tuple(w, u, v, p);
  }
  sort(E.begin(), E.end());
  vector<vector<pair<int, int>>> E2(N);
  unionfind UF(N);
  long long S = 0;
  for (int i = 0; i < K; i++){
    int u = get<1>(E[i]);
    int v = get<2>(E[i]);
    if (!UF.same(u, v)){
      int w = get<0>(E[i]);
      S += w;
      UF.unite(u, v);
      E2[u].push_back(make_pair(w, v));
      E2[v].push_back(make_pair(w, u));
    }
  }
  if (S > C){
    cout << -1 << endl;
  } else {
    vector<int> p(N, -1);
    vector<int> pw(N, 0);
    vector<int> d(N, 0);
    queue<int> Q;
    Q.push(0);
    while (!Q.empty()){
      int v = Q.front();
      Q.pop();
      for (pair<int, int> P : E2[v]){
        int w = P.second;
        if (w != p[v]){
          p[w] = v;
          d[w] = d[v] + 1;
          pw[w] = P.first;
          Q.push(w);
        }
      }
    }
    vector<vector<int>> pp(LOG, vector<int>(N, -1));
    vector<vector<int>> bl(LOG, vector<int>(N, 0));
    for (int i = 0; i < N; i++){
      pp[0][i] = p[i];
      bl[0][i] = pw[i];
    }
    for (int i = 0; i < LOG - 1; i++){
      for (int j = 0; j < N; j++){
        if (pp[i][j] != -1){
          pp[i + 1][j] = pp[i][pp[i][j]];
          bl[i + 1][j] = max(bl[i][j], bl[i][pp[i][j]]);
        }
      }
    }
    auto get_max = [&](int u, int v){
      if (d[u] > d[v]){
        swap(u, v);
      }
      int ans = 0;
      for (int i = 0; i < LOG; i++){
        if (((d[v] - d[u]) >> i & 1) == 1){
          ans = max(ans, bl[i][v]);
          v = pp[i][v];
        }
      }
      if (u == v){
        return ans;
      }
      for (int i = LOG - 1; i >= 0; i--){
        if (pp[i][u] != pp[i][v]){
          ans = max(ans, bl[i][u]);
          ans = max(ans, bl[i][v]);
          u = pp[i][u];
          v = pp[i][v];
        }
      }
      ans = max(ans, pw[u]);
      ans = max(ans, pw[v]);
      return ans;
    };
    int ans = 0;
    for (int i = 0; i < K; i++){
      int w = get<0>(E[i]);
      int u = get<1>(E[i]);
      int v = get<2>(E[i]);
      if (S - get_max(u, v) + w <= C){
        ans = max(ans, get<3>(E[i]));
      }
    }
    cout << ans << endl;
  }
}
0