Submission #31634

# Submission time Handle Problem Language Result Execution time Memory
31634 2017-08-30T05:03:31 Z cheater2k Beads and wires (APIO14_beads) C++14
0 / 100
10 ms 9728 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200010;
const int inf = 2e9 + 12345;

int n;
int f[N][2];
vector < pair<int,int> > G[N], ch[N];
int ans;

void dfs(int u, int p) {
	for (auto edge: G[u]) if (edge.second != p) {
		dfs(edge.second, u);
		ch[u].push_back(edge);
	}
	f[u][1] = -inf;
	f[u][0] = 0;
	for (auto edge: ch[u]) {
		f[u][0] += max(f[edge.second][0], f[edge.second][1] + edge.first);
	}
	for (auto edge: ch[u]) {
		int mx = f[u][0];
		mx -= max(f[edge.second][0], f[edge.second][1] + edge.first);
		mx += (f[edge.second][0] + edge.first);
		f[u][1] = max(f[u][1], mx);
	}
}

void re_dfs(int u, int p) {
	ans = max(ans, f[u][0]);

	// prepare
	vector <int> L(n + 2, 0);
	vector <int> R(n + 2, 0);

	for (int i = 0; i < (int)ch[u].size(); ++i) {
		pair<int,int> edge = ch[u][i];
		L[i + 1] = max(L[i], f[u][0] - max(f[edge.second][0], f[edge.second][1] + edge.first) + (f[edge.second][0] + edge.first));
	}
	for (int i = (int)ch[u].size() - 1; i >= 0; --i) {
		pair<int,int> edge = ch[u][i];
		R[i] = max(R[i + 1], f[u][0] - max(f[edge.second][0], f[edge.second][1] + edge.first) + (f[edge.second][0] + edge.first));
	}

	// solve
	for (int i = 0; i < (int)ch[u].size(); ++i) {
		int v = ch[u][i].second, w = ch[u][i].first;
		int cost = max(f[v][0], f[v][1] + w);
		int fu0 = f[u][0] - cost;
		int fu1 = max(L[i], R[i + 1]) - cost;
		
		int fv0 = f[v][0] + max(fu0, fu1 + w);
		int fv1 = max(f[v][1] + max(fu0, fu1 + w), fv0 - max(fu0, fu1 + w) + (fu0 + w));
		f[v][0] = fv0; f[v][1] = fv1;

		re_dfs(v, u);
 	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i < n; ++i) {
		int u, v, w; cin >> u >> v >> w;
		G[u].push_back(make_pair(w,v)); G[v].push_back(make_pair(w,u));
	}

	dfs(1,1);
	re_dfs(1,1);

	cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 10 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 10 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 10 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 10 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -