답안 #31595

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
31595 2017-08-30T02:32:14 Z aome 구슬과 끈 (APIO14_beads) C++14
0 / 100
8 ms 8192 KB
/*input
10
4 10 2 
1 2 21 
1 3 13 
6 7 1 
7 9 5  
2 4 3  
2 5 8 
1 6 55 
6 8 34
5
1 2 10
1 3 40
1 4 15
1 5 20
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N 200005

int n;
vector<vector<pair<int, int> > > a(N);
int wpar[N];
int dp[N][2];

void dfs(int u, int p) {
	for (int i = 0; i < a[u].size(); i++) {
		int v = a[u][i].fi; if (v == p) continue;
		dfs(v, u); wpar[v] = a[u][i].se;
	}
}

int cal(int u, int p, bool up) {
	int &ret = dp[u][up];
	if (ret != -1) return ret;
	ret = 0;
	int sum = 0;
	for (int i = 0; i < a[u].size(); i++) {
		int v = a[u][i].fi; if (v == p) continue;
		sum += cal(v, u, 0);
	}
	for (int i = 0; i < a[u].size(); i++) {
		if (a[u][i].fi == p) continue;
		for (int j = i + 1; j < a[u].size(); j++) {
			if (a[u][j].fi == p) continue;
			int l = a[u][i].fi; int r = a[u][j].fi;
			int w = a[u][i].se + a[u][j].se;
			ret = max(ret, sum - cal(l, u, 0) - cal(r, u, 0) + w + cal(l, u, 1) + cal(r, u, 1));
		}
	}
	if (!up) {
		for (int i = 0; i < a[u].size(); i++) {
			int v = a[u][i].fi;
			if (v == p) continue;
			ret = max(ret, sum - cal(v, u, 0) + a[u][i].se + wpar[u] + cal(v, u, 1));
		}
	}
	return ret;
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n - 1; i++) {
		int u, v, c; cin >> u >> v >> c;
		a[u].push_back(mp(v, c));
		a[v].push_back(mp(u, c));
	}
	dfs(1, 1);
	memset(dp, -1, sizeof(dp));
	cout << cal(1, 1, 1) << endl;
}

Compilation message

beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < a[u].size(); i++) {
                  ~~^~~~~~~~~~~~~
beads.cpp: In function 'long long int cal(long long int, long long int, bool)':
beads.cpp:65:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < a[u].size(); i++) {
                  ~~^~~~~~~~~~~~~
beads.cpp:69:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < a[u].size(); i++) {
                  ~~^~~~~~~~~~~~~
beads.cpp:71:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = i + 1; j < a[u].size(); j++) {
                       ~~^~~~~~~~~~~~~
beads.cpp:79:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < a[u].size(); i++) {
                   ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8192 KB Output is correct
2 Correct 8 ms 8192 KB Output is correct
3 Incorrect 8 ms 8192 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8192 KB Output is correct
2 Correct 8 ms 8192 KB Output is correct
3 Incorrect 8 ms 8192 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8192 KB Output is correct
2 Correct 8 ms 8192 KB Output is correct
3 Incorrect 8 ms 8192 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8192 KB Output is correct
2 Correct 8 ms 8192 KB Output is correct
3 Incorrect 8 ms 8192 KB Output isn't correct
4 Halted 0 ms 0 KB -