結果

問題 No.1488 Max Score of the Tree
ユーザー MZKiMZKi
提出日時 2021-04-05 01:48:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,810 bytes
コンパイル時間 1,346 ms
コンパイル使用メモリ 151,608 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-28 13:10:57
合計ジャッジ時間 3,010 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 4 ms
4,380 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 2 ms
4,376 KB
testcase_22 WA -
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 32 ms
4,380 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

struct edge{
    ll to, cost;
};

ll now = 0;
void dfs(const vector<vector<edge>> &G, int v, int p, int d){
    int cnt = 0;
    for(auto nv : G[v]){
        if(nv.to == p)continue;
        dfs(G, nv.to, v, d + nv.cost);
        cnt++;
    }
    if(cnt == 0)now += d;
}

ll solve(int n, int k, vector<int> a, vector<int> b, vector<int> c){
    ll ret = 0;
    for(int bit = 0; bit < (1LL<<(n-1)); bit++){
        ll sum = 0;
        vector<vector<edge>> G(n);
        rep(i, n-1)if(bit & (1<<i))sum += c[i];
        rep(i, n-1){
            if(bit & (1<<i)){
                G[a[i]].push_back({b[i], c[i]*2});
                G[b[i]].push_back({a[i], c[i]*2});
            }else{
                G[a[i]].push_back({b[i], c[i]});
                G[b[i]].push_back({a[i], c[i]});
            }
        }
        if(sum > k)continue;
        dfs(G, 0, -1, 0);
        chmax(ret, now);
        now = 0;
    }
    return ret;
}

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n-1), b(n-1), c(n-1);
    rep(i, n-1){
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }
    if(n >= 21){
        cout << -1 << endl;
        return 0;
    }
    cout << solve(n, k, a, b, c);
}
0