Submission #674461

# Submission time Handle Problem Language Result Execution time Memory
674461 2022-12-24T10:27:20 Z pavement Beads and wires (APIO14_beads) C++17
0 / 100
5 ms 8152 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

int n, mem[200005][2];
vector<ii> adj[200005];

int dp(int n, bool b, int e = -1) {
	if (mem[n][b] != -1) return mem[n][b];
	if (adj[n].size() - (e != -1) == 0) {
		// leaf node
		return b ? -(int)1e16 : 0;
	}
	int case_1 = 0, case_2 = -(int)1e16;
	vector<ii> reps;
	for (auto [u, w] : adj[n]) if (u != e) {
		int curr = max(dp(u, 0, n), dp(u, 1, n) + w);
		case_1 += curr;
		reps.eb(dp(u, 0, n) + w - curr, u);
	}
	sort(reps.begin(), reps.end(), greater<ii>());
	if (b) {
		for (auto [u, w] : adj[n]) if (u != e) {
			int curr = max(dp(u, 0, n), dp(u, 1, n) + w);
			int m_val = -(int)1e16, s_val = -(int)1e16;
			for (auto [v, idx] : reps) if (idx != u) {
				if (v > s_val) s_val = v;
				if (s_val > m_val) swap(m_val, s_val);
				if (s_val != -(int)1e16) break;
			}
			int extra = max(0ll, m_val + s_val);
			case_2 = max(case_2, extra + case_1 + dp(u, 0, n) + w - curr);
		}
		case_1 = -(int)1e16;
	} else {
		if ((int)reps.size() >= 2) {
			case_2 = case_1 + reps[0].first + reps[1].first;
		}
	}
	return mem[n][b] = max(case_1, case_2);
}

main() {
	memset(mem, -1, sizeof mem);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 1, u, v, w; i < n; i++) {
		cin >> u >> v >> w;
		adj[u].eb(v, w);
		adj[v].eb(u, w);
	}
	cout << dp(1, 0) << '\n';
}

Compilation message

beads.cpp:64:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   64 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8088 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Incorrect 4 ms 8152 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8088 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Incorrect 4 ms 8152 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8088 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Incorrect 4 ms 8152 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8088 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Incorrect 4 ms 8152 KB Output isn't correct
7 Halted 0 ms 0 KB -