結果

問題 No.1488 Max Score of the Tree
ユーザー jutamajutama
提出日時 2021-04-23 23:49:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 4,354 bytes
コンパイル時間 3,558 ms
コンパイル使用メモリ 193,220 KB
実行使用メモリ 82,344 KB
最終ジャッジ日時 2023-09-17 13:14:50
合計ジャッジ時間 5,894 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
78,804 KB
testcase_01 AC 69 ms
75,644 KB
testcase_02 AC 74 ms
79,592 KB
testcase_03 AC 75 ms
82,248 KB
testcase_04 AC 77 ms
82,344 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 21 ms
22,944 KB
testcase_07 AC 46 ms
48,500 KB
testcase_08 AC 31 ms
34,708 KB
testcase_09 AC 25 ms
25,480 KB
testcase_10 AC 46 ms
49,876 KB
testcase_11 AC 81 ms
82,340 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 13 ms
14,444 KB
testcase_14 AC 37 ms
40,888 KB
testcase_15 AC 24 ms
26,628 KB
testcase_16 AC 5 ms
7,520 KB
testcase_17 AC 16 ms
17,912 KB
testcase_18 AC 51 ms
55,880 KB
testcase_19 AC 30 ms
32,448 KB
testcase_20 AC 13 ms
15,028 KB
testcase_21 AC 7 ms
9,100 KB
testcase_22 AC 24 ms
26,008 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 19 ms
21,052 KB
testcase_27 AC 4 ms
5,736 KB
testcase_28 AC 10 ms
11,620 KB
testcase_29 AC 13 ms
14,488 KB
testcase_30 AC 62 ms
66,716 KB
testcase_31 AC 78 ms
82,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bitset>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace atcoder;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, ld> pid;
typedef pair<ll, string> pls;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<vector<int>>>> vvvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<vector<vector<vector<ll>>>> vvvvl;
typedef vector<ld> vd;
typedef vector<vector<ld>> vvd;
typedef vector<string> vs;
#define rep(i,n) for(auto i=0; i<n; ++i)
#define repm(i,s,n) for(auto i=s; i<n; ++i)
#define repd(i,n) for(auto i=n-1; i>=0; --i)
#define repdm(i,e,n) for(auto i=n-1; i>=e; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template <class mint, internal::is_modint_t<mint>* = nullptr>
constexpr istream& operator>>(istream& is, mint& x) noexcept {long long v = 0; std::cin >> v; x = v; return is;}
template <class mint, internal::is_modint_t<mint>* = nullptr>
constexpr ostream& operator<<(ostream& os, const mint& x) noexcept {os << x.val(); return os;}
inline void _n() { std::cout << std::endl; }
template <class T> inline void _(const T a) { std::cout << a; }
template <class T> inline void _l(const T a) { _(a); _n(); }
template <class T> inline void _s(const T a) { _(a); _(' '); }
template <class T> inline void _v(const std::vector<T> v) { for(auto a : v) _(a); }
template <class T> inline void _vl(const std::vector<T> v) { for(auto a : v) _l(a); }
template <class T> inline void _vs(const std::vector<T> v) { for(auto a : v) _s(a); _n(); }
template <class T> inline void _vvl(const std::vector<std::vector<T>> v) { for(auto a : v) { _v(a); _n(); } }
template <class T> inline void _vvs(const std::vector<std::vector<T>> v) { for(auto a : v) { _vs(a); } }
inline void ynl(const bool b) {_l(b ? "Yes" : "No");}
inline void ynu(const bool b) {_l(b ? "YES" : "NO");}

const int INF = 1 << 30;
const long long INF_LL = 1LL << 62;
const long long MOD1 = 1000000007;
const long long MOD9 = 998244353;
using mint1 = modint1000000007;
using mint9 = modint998244353;

//* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//
struct edge {
    int idx;
    int to;
    ll cost;
};

int N;
int K;
vector<vector<edge>> edges;
vector<ll> e_cnt;
vector<ll> e_cost;
vector<int> visit;

void input() {
   
    cin >> N >> K;
    edges.resize(N, vector<edge>());
    e_cnt.resize(N-1);
    e_cost.resize(N-1);
    visit.resize(N);
    rep(i, N-1) {
        int a, b; cin >> a >> b; a--; b--;
        ll c; cin >> c;
        edges[a].push_back({i, b, c});
        edges[b].push_back({i, a, c});
        e_cost[i] = c;
    }
    
}

int dfs(int v, int ei = -1) {
    
    int cnt = 0;
    bool leaf = 1;
    visit[v] = 1;
    
    for(auto e : edges[v]) {
        if(!visit[e.to]) {
            leaf = 0;
            cnt += dfs(e.to, e.idx);
        }
    }
    
    if(ei != -1) {
        e_cnt[ei] = cnt + leaf;
    }
    
    return cnt + leaf;
}


void solve() {
    
    dfs(0);
    // _vs(e_cnt);
    
    ll ans = 0;
    vvl dp(N, vl(K+1));
    rep(i, N-1) {
        ans += e_cost[i] * e_cnt[i];
        rep(j, K+1) {
            chmax(dp[i+1][j], dp[i][j]);
            if(j + e_cost[i] <= K) chmax(dp[i+1][j + e_cost[i]], dp[i][j] + e_cost[i] * e_cnt[i]);
        }
    }
    
    ll tmp = 0;
    rep(j, K+1) {
        chmax(tmp, dp[N-1][j]);
    }
    ans += tmp;
    _l(ans);
    
    
    
    
}

//* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//

int main() {
    
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
    std::cin.tie(0);
    ios::sync_with_stdio(false);
    
    input();
    solve();
    
    return 0;
}
0