結果

問題 No.1488 Max Score of the Tree
ユーザー shimarutshimarut
提出日時 2021-04-23 23:05:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 3,938 bytes
コンパイル時間 3,450 ms
コンパイル使用メモリ 188,868 KB
実行使用メモリ 42,800 KB
最終ジャッジ日時 2023-09-17 13:00:43
合計ジャッジ時間 5,460 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
41,108 KB
testcase_01 AC 47 ms
39,520 KB
testcase_02 AC 50 ms
41,928 KB
testcase_03 AC 53 ms
42,644 KB
testcase_04 AC 52 ms
42,800 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 15 ms
13,124 KB
testcase_07 AC 33 ms
25,748 KB
testcase_08 AC 22 ms
18,932 KB
testcase_09 AC 17 ms
14,128 KB
testcase_10 AC 32 ms
26,536 KB
testcase_11 AC 55 ms
42,764 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 9 ms
8,588 KB
testcase_14 AC 26 ms
22,108 KB
testcase_15 AC 17 ms
14,976 KB
testcase_16 AC 5 ms
5,348 KB
testcase_17 AC 11 ms
10,564 KB
testcase_18 AC 36 ms
29,620 KB
testcase_19 AC 20 ms
17,876 KB
testcase_20 AC 9 ms
8,852 KB
testcase_21 AC 5 ms
6,072 KB
testcase_22 AC 17 ms
14,496 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 13 ms
12,016 KB
testcase_27 AC 4 ms
4,480 KB
testcase_28 AC 7 ms
7,440 KB
testcase_29 AC 9 ms
8,640 KB
testcase_30 AC 42 ms
35,044 KB
testcase_31 AC 54 ms
42,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <iomanip>
#include <cstdint>
#include <bitset>
#include <sstream>
#include <regex>
#include <fstream>
#include <array>
#include <atcoder/all>

using namespace atcoder;
using mint = modint1000000007;
//using mint = modint998244353;
//using mint = modint;

#pragma region using
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vmint = vector<mint>;
using vvmint = vector<vmint>;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using vpint = vector<pint>;
using vvpint = vector<vpint>;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#pragma endregion
#pragma region UnionFind
struct UnionFind
{
	vector<int> par;

	UnionFind(int n) : par(n, -1) {}
	void init(int n) { par.assign(n, -1); }

	int root(int x)
	{
		if (par[x] < 0) return x;
		else return par[x] = root(par[x]);
	}

	bool issame(int x, int y)
	{
		return root(x) == root(y);
	}

	bool merge(int x, int y)
	{
		x = root(x); y = root(y);
		if (x == y) return false;
		if (par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}

	int size(int x)
	{
		return -par[root(x)];
	}
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b)
{
	if (b == 0)return a;
	return gcd(b, a%b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b)
{
	return a / gcd(a, b) * b;
}
#pragma endregion
#pragma region chmin
template<class T> inline bool chmin(T& a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region chmax
template<class T> inline bool chmax(T& a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region Dijkstra
vl dijkstra(vector<vector<pair<int, ll>>> v, int s)
{
	ll INF = 1e18;
	int MAX = 1e6;
	vl res(MAX, INF);
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
	q.push({ 0,s });
	while (!q.empty())
	{
		int now;
		ll d;
		tie(d, now) = q.top();
		q.pop();
		if (!chmin(res[now], d))continue;
		for (auto p : v[now])
		{
			int next;
			ll c;
			tie(next, c) = p;
			if (res[next] <= res[now] + c)continue;
			q.push({ res[now] + c,next });
		}
	}
	return res;
}
#pragma endregion
//グリッド内チェック
bool out(int x, int y, int h, int w)
{
	if (x < 0 || h <= x || y < 0 || w <= y)return true;
	else return false;
}
#pragma endregion

vvi v(101);
vi cnt(101);
int s = 0;
vvpint v2(101);
vi d(101);

struct edge
{
	int a;
	int b;
	int c;
};

int dfs(int now, int p)
{
	bool f = true;
	for (int next : v[now])
	{
		if (next == p)continue;
		cnt[now] += dfs(next, now);
		f = false;
	}
	if (f)cnt[now] = 1;
	return cnt[now];
}

void dfs2(int now, int p, int sum)
{
	d[now] = sum;
	bool f = true;
	for (auto q : v2[now])
	{
		int next, l; tie(next, l) = q;
		if (next == p)continue;
		dfs2(next, now, sum + l);
		f = false;
	}
	if (f)s += d[now];
}

int main()
{
	int n, k; cin >> n >> k;
	vector<edge> e(n - 1);
	rep(i, n - 1)
	{
		int a, b, c; cin >> a >> b >> c;
		--a, --b;
		v[a].push_back(b);
		v[b].push_back(a);
		e[i] = { a,b,c };
		v2[a].push_back({ b,c });
		v2[b].push_back({ a,c });
	}
	dfs(0, -1);
	dfs2(0, -1, 0);
	vi w(n - 1), val(n - 1);
	rep(i, n - 1)
	{
		auto t = e[i];
		int a = t.a, b = t.b, c = t.c;
		w[i] = c, val[i] = c * min(cnt[a], cnt[b]);
	}
	vvi dp(n, vi(k + 1));
	rep(i, n - 1)rep(j, k + 1)
	{
		chmax(dp[i + 1][j], dp[i][j]);
		if (j + w[i] <= k)chmax(dp[i + 1][j + w[i]], dp[i][j] + val[i]);
	}
	cout << s + *max_element(all(dp[n - 1])) << endl;
}
0