답안 #31635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
31635 2017-08-30T05:05:46 Z cheater2k 구슬과 끈 (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, -inf);
	vector <int> R(n + 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;
		
		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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 9 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 9 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 9 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 9 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -