Submission #31646

# Submission time Handle Problem Language Result Execution time Memory
31646 2017-08-30T08:33:34 Z cheater2k Beads and wires (APIO14_beads) C++14
57 / 100
289 ms 24224 KB
#include <bits/stdc++.h>
using namespace std;

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

int n;
int f[N][2], g[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, g[u][0] + f[u][0]);
	//cerr << "redfs " << u << ' ' << f[u][0] + g[u][0] << ' ' << f[u][1] + g[u][1] << endl;

	// prepare
	vector <int> L(ch[u].size() + 2, -inf);
	vector <int> R(ch[u].size() + 2, -inf);

	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;
		g[v][0] = 0, g[v][1] = -inf;
		// g[v][0]
		g[v][0] = max(g[v][0], fu0 + g[u][0]);
		g[v][0] = max(g[v][0], fu1 + g[u][0] + w);
		g[v][0] = max(g[v][0], fu0 + g[u][1] + w);

		// g[v][1]
		g[v][1] = max(g[v][1], g[u][0] + fu0 + w);

		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);
	g[1][1] = -inf;
	re_dfs(1,1);

	cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 10 ms 9804 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 10 ms 9804 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 9 ms 9728 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
22 Correct 14 ms 9700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 10 ms 9804 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 9 ms 9728 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
22 Correct 14 ms 9700 KB Output is correct
23 Correct 12 ms 10112 KB Output is correct
24 Correct 13 ms 10112 KB Output is correct
25 Correct 13 ms 10112 KB Output is correct
26 Correct 17 ms 10496 KB Output is correct
27 Correct 17 ms 10496 KB Output is correct
28 Correct 16 ms 10496 KB Output is correct
29 Correct 16 ms 10496 KB Output is correct
30 Correct 15 ms 10496 KB Output is correct
31 Correct 17 ms 11136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 10 ms 9804 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 9 ms 9728 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
22 Correct 14 ms 9700 KB Output is correct
23 Correct 12 ms 10112 KB Output is correct
24 Correct 13 ms 10112 KB Output is correct
25 Correct 13 ms 10112 KB Output is correct
26 Correct 17 ms 10496 KB Output is correct
27 Correct 17 ms 10496 KB Output is correct
28 Correct 16 ms 10496 KB Output is correct
29 Correct 16 ms 10496 KB Output is correct
30 Correct 15 ms 10496 KB Output is correct
31 Correct 17 ms 11136 KB Output is correct
32 Correct 56 ms 13304 KB Output is correct
33 Correct 54 ms 13336 KB Output is correct
34 Correct 53 ms 13308 KB Output is correct
35 Incorrect 289 ms 24224 KB Output isn't correct
36 Halted 0 ms 0 KB -