Submission #1338535

#TimeUsernameProblemLanguageResultExecution timeMemory
1338535ppmn_6Beads and wires (APIO14_beads)C++20
0 / 100
1 ms344 KiB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<class Key, class Compare = less<Key>>
using indexed_set = tree<Key, null_type, Compare, rb_tree_tag, tree_order_statistics_node_update>;

template<class Key, class Value, class Compare = less<Key>>
using indexed_map = tree<Key, Value, Compare, rb_tree_tag, tree_order_statistics_node_update>;

template<class Key>
using hash_set = gp_hash_table<
    Key, null_type, hash<Key>, equal_to<Key>, direct_mask_range_hashing<Key>, linear_probe_fn<>,
    hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>>;
    
template<class Key, class Value>
using hash_map = gp_hash_table<
    Key, Value, hash<Key>, equal_to<Key>, direct_mask_range_hashing<Key>, linear_probe_fn<>,
    hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>>;
 
// https://codeforces.com/blog/entry/79148
class Timer: chrono::high_resolution_clock {
    const time_point start_time;
public:
    Timer(): start_time(now()) {}
    rep elapsed_time() const {
		return chrono::duration_cast<chrono::milliseconds>(now() - start_time).count();
	}
} timer;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, ans = 0;
	cin >> n;
	vector<vector<pair<int, int>>> tree(n + 1);
	vector<int> dp0, dp1;
	for (int i = 1; i < n; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		tree[u].push_back({v, w});
		tree[v].push_back({u, w});
	}
	auto dfs = [&] (auto self, int u, int p)->void {
		int wp, b1 = -2e9, b2 = -2e9;
		for (auto &[v, w] : tree[u]) {
			if (v != p) {
				self(self, v, u);
				dp0[u] += max(dp0[v], dp1[v]);
				if (w - max(0, dp1[v] - dp0[v]) >= b1) {
					b2 = b1;
					b1 = w - max(0, dp1[v] - dp0[v]);
				}
				else if (w - max(0, dp1[v] - dp0[v]) >= b2) {
					b2 = w - max(0, dp1[v] - dp0[v]);
				}
			}
			else {
				wp = w;
			}
		}
		if (p) {
			for (auto &[v, w] : tree[u]) {
				if (v != p) {
					dp1[u] = max(dp1[u], wp + w + dp0[u] - max(dp0[v], dp1[v]));
				}
			}
		}
		if (b1 != -2e9 && b2 != -2e9) {
			dp0[u] = max(dp0[u], dp0[u] + b1 + b2);
		}
	};
	for (int i = 1; i <= n; i++) {
		dp0.assign(n + 1, 0);
		dp1.assign(n + 1, 0);
		dfs(dfs, i, 0);
		ans = max(ans, dp0[i]);
	}
	cout << ans;
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...