結果

問題 No.2642 Don't cut line!
ユーザー tnakao0123tnakao0123
提出日時 2024-02-21 10:11:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,701 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 112,144 KB
実行使用メモリ 25,828 KB
最終ジャッジ日時 2024-09-29 04:12:41
合計ジャッジ時間 4,533 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 93 ms
25,828 KB
testcase_02 AC 94 ms
25,708 KB
testcase_03 AC 94 ms
25,708 KB
testcase_04 AC 92 ms
25,764 KB
testcase_05 AC 91 ms
25,716 KB
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 WA -
testcase_16 AC 60 ms
12,868 KB
testcase_17 AC 76 ms
24,892 KB
testcase_18 AC 82 ms
25,276 KB
testcase_19 AC 61 ms
23,688 KB
testcase_20 AC 41 ms
15,764 KB
testcase_21 AC 38 ms
9,668 KB
testcase_22 AC 42 ms
10,688 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 3 ms
11,976 KB
testcase_34 AC 3 ms
11,844 KB
testcase_35 AC 2 ms
6,816 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:138:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  138 |     auto [w, p, u, v] = es[i];
      |          ^
main.cpp:157:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  157 |       auto [v, ei] = nbru[cis[u]++];
      |            ^
main.cpp:181:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  181 |       auto [w, p, u, v] = es[i];
      |            ^

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2642.cc:  No.2642 Don't cut line! - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
#include<tuple>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int BN = 17;
const int MAX_K = 100000;
const int MAX_P = 100000;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;

/* typedef */

typedef long long ll;
typedef queue<int> qi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef tuple<int,int,int,int> tp4;

struct UFT {
  vector<int> links, ranks, sizes;
  UFT() {}

  void init(int n) {
    links.resize(n);
    for (int i = 0; i < n; i++) links[i] = i;
    ranks.assign(n, 1);
    sizes.assign(n, 1);
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

tp4 es[MAX_K];
bool used[MAX_K];
UFT uft;
vpii nbrs[MAX_N];
int ps[MAX_N][BN], pws[MAX_N][BN], ds[MAX_N], cis[MAX_N];

/* subroutines */

int find_maxw(int u, int v) {
  if (ds[u] > ds[v]) swap(u, v);

  int maxw = 0;
  for (int i = BN - 1; i >= 0; i--)
    if (((ds[v] - ds[u]) >> i) & 1) {
      maxw = max(maxw, pws[v][i]);
      v = ps[v][i];
    }
  if (u == v) return maxw;

  for(int i = BN - 1; i >= 0; i--)
    if (ps[u][i] != ps[v][i]) {
      maxw = max(maxw, max(pws[u][i], pws[v][i]));
      u = ps[u][i], v = ps[v][i];
    }

  return max(maxw, max(pws[u][0], pws[v][0]));
}

/* main */

int main() {
  int n, k;
  ll c;
  scanf("%d%d%lld", &n, &k, &c);

  for (int i = 0; i < k; i++) {
    int u, v, w, p;
    scanf("%d%d%d%d", &u, &v, &w, &p);
    u--, v--;
    es[i] = {w, p, u, v};
  }
  sort(es, es + k);
  
  uft.init(n);
  int maxp = 0;
  ll wsum = 0;
  for (int i = 0; i < k; i++) {
    auto [w, p, u, v] = es[i];
    if (! uft.same(u, v)) {
      used[i] = true;
      uft.merge(u, v);
      maxp = p;
      wsum += w;
      nbrs[u].push_back({v, i});
      nbrs[v].push_back({u, i});
    }
  }
  if (wsum > c) { puts("-1"); return 0; }
  //printf("maxp=%d, wsum=%lld\n", maxp, wsum);

  ps[0][0] = -1, pws[0][0] = 0, ds[0] = 0;
  for (int u = 0; u >= 0;) {
    auto &nbru = nbrs[u];
    int up = ps[u][0];

    if (cis[u] < nbru.size()) {
      auto [v, ei] = nbru[cis[u]++];
      if (v != up) {
	ps[v][0] = u, pws[v][0] = get<1>(es[ei]), ds[v] = ds[u] + 1;
	u = v;
      }
    }
    else
      u = up;
  }

  for (int i = 0; i < BN - 1; i++)
    for (int u = 0; u < n; u++) {
      if (ps[u][i] >= 0) {
	ps[u][i + 1] = ps[ps[u][i]][i];
	pws[u][i + 1] = max(pws[u][i], pws[ps[u][i]][i]);
      }
      else {
	ps[u][i + 1] = -1;
	pws[u][i + 1] = 0;
      }
    }

  for (int i = 0; i < k; i++)
    if (! used[i]) {
      auto [w, p, u, v] = es[i];
      if (maxp < p) {
	int mw = find_maxw(u, v);
	if (wsum + w - mw <= c) maxp = p;
      }
    }

  printf("%d\n", maxp);
  
  return 0;
}
0