結果

問題 No.1488 Max Score of the Tree
ユーザー algon_320algon_320
提出日時 2021-04-23 21:58:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 4,699 bytes
コンパイル時間 1,786 ms
コンパイル使用メモリ 177,212 KB
実行使用メモリ 82,316 KB
最終ジャッジ日時 2023-09-17 12:10:32
合計ジャッジ時間 4,212 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
78,804 KB
testcase_01 AC 76 ms
75,772 KB
testcase_02 AC 81 ms
79,592 KB
testcase_03 AC 84 ms
82,296 KB
testcase_04 AC 84 ms
82,296 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 23 ms
22,876 KB
testcase_07 AC 50 ms
48,508 KB
testcase_08 AC 34 ms
34,668 KB
testcase_09 AC 27 ms
25,540 KB
testcase_10 AC 50 ms
49,836 KB
testcase_11 AC 86 ms
82,188 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 14 ms
14,388 KB
testcase_14 AC 41 ms
41,036 KB
testcase_15 AC 25 ms
26,528 KB
testcase_16 AC 6 ms
7,472 KB
testcase_17 AC 16 ms
17,492 KB
testcase_18 AC 55 ms
55,972 KB
testcase_19 AC 32 ms
32,420 KB
testcase_20 AC 14 ms
14,984 KB
testcase_21 AC 8 ms
9,108 KB
testcase_22 AC 25 ms
26,068 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 20 ms
21,032 KB
testcase_27 AC 4 ms
5,732 KB
testcase_28 AC 11 ms
11,496 KB
testcase_29 AC 14 ms
14,340 KB
testcase_30 AC 70 ms
66,676 KB
testcase_31 AC 89 ms
82,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T>
using heap = priority_queue<T, deque<T>, greater<T>>;
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define fore(x, c) for (auto &&x : c)
#define rep(i, a, n) for (int i = (int)(a), i##len = (int)(n); i < i##len; ++i)
#define rrep(i, a, n) for (int i = (int)(n - 1); i >= a; --i)
#define pb push_back
#define eb emplace_back
#define dump(...)
const signed INF_ = 1001001001;
const long long INF = 1001001001001001001LL;
const int DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
          DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class C>
int sz(const C &c) {
  return (int)c.size();
}
template <class C, class T>
bool contains(const C &c, const T &v) {
  return c.find(v) != c.end();
}
template <class T>
bool inseg(const T &l, const T &x, const T &r) {
  return l <= x && x < r;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << " " << p.second;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}
template <class T, class C>
ostream &ostream_container_impl(ostream &os, const C &v) {
  if (v.empty()) return os;
  auto itr = begin(v), prv = prev(end(v));
  while (itr != prv) os << *itr++ << " ";
  return os << *prv;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  return ostream_container_impl<T>(os, v);
}
template <class T>
ostream &operator<<(ostream &os, const deque<T> &v) {
  return ostream_container_impl<T>(os, v);
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &v) {
  return ostream_container_impl<T>(os, v);
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}
template <class T, class U>
bool chmax(T &a, const U &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
void psum(T &c) {
  partial_sum(begin(c), end(c), begin(c));
}
template <class T>
void decrement(T &x) {
  x -= 1;
}
template <class T, class U>
void decrement(pair<T, U> &p) {
  decrement(p.first);
  decrement(p.second);
}
template <class T>
void decrement(vector<T> &v) {
  for (auto &x : v) decrement(x);
}
template <class T>
void increment(T &x) {
  x += 1;
}
template <class T, class U>
void incrment(pair<T, U> &p) {
  increment(p.first);
  increment(p.second);
}
template <class T>
void increment(vector<T> &v) {
  for (auto &x : v) increment(x);
}

#if defined(LOCAL)
void dump_impl(string s) { assert(s.empty()); }
template <class H, class... T>
void dump_impl(string s, const H &head, const T &...tail) {
  int par = 0;
  rep(i, 0, sz(s)) {
    char ch = s[i];
    if (ch == ',' && par == 0) {
      cerr << " = " << head << ",";
      dump_impl(s.substr(i + 1), tail...);
      return;
    } else {
      cerr << ch;
      if (ch == '(') par++;
      if (ch == ')') par--;
    }
  }
}
#ifdef dump
#undef dump
#endif
#define dump(...)                             \
  do {                                        \
    cerr << "\x1b[33;1m";                     \
    dump_impl(#__VA_ARGS__ ",", __VA_ARGS__); \
    cerr << "\x1b[0m" << endl;                \
  } while (0)
#endif

struct before_main_function {
  before_main_function() {
#if defined(LOCAL) && defined(int)
    cerr << "\x1b[7m"
         << "'int' is 'long long'"
         << "\x1b[m" << endl;
#endif
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(15) << fixed;
  }
} before_main_function;
//------------------8<------------------------------------8<--------------------

signed main() {
  int N, K;
  cin >> N >> K;

  vi L(N - 1), C(N - 1);
  vector<vector<pii>> T(N);
  rep(i, 0, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    L[i] = c;
    a--, b--;
    T[a].eb(b, i);
    T[b].eb(a, i);
  }

  auto dfs = [&](auto &f, int v, int p) -> int {
    int ret = 0;
    for (pii e : T[v]) {
      int w, i;
      tie(w, i) = e;
      if (w == p) continue;
      int t = f(f, w, v);
      C[i] += t;
      ret += t;
    }
    if (v && sz(T[v]) == 1) ret += 1;
    return ret;
  };
  dfs(dfs, 0, -1);

  vvi dp(N, vi(K + 1));
  rep(i, 0, N - 1) {
    rep(j, 0, K + 1) {
      chmax(dp[i + 1][j], dp[i][j] + L[i] * C[i]);
      if (j + L[i] <= K) {
        chmax(dp[i + 1][j + L[i]], dp[i][j] + 2 * L[i] * C[i]);
      }
    }
  }
  int ans = 0;
  rep(i, 0, K + 1) { chmax(ans, dp[N - 1][i]); }
  cout << ans << endl;
}
0