답안 #64322

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
64322 2018-08-04T05:16:39 Z kingpig9 구슬과 끈 (APIO14_beads) C++11
0 / 100
16 ms 14564 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5 + 10;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))

void setmax (int &a, int b) {
	if (a < b) {
		a = b;
	}
}

int N;
vector<int> adj[MAXN];
map<int, int> weight[MAXN];
int W[MAXN];
int dp[MAXN][2];

void dfs (int x) {
	int totc = 0;
	for (int y : adj[x]) {
		adj[y].erase(find(all(adj[y]), x));
		W[y] = weight[x][y];
		dfs(y);
		totc += dp[y][0];
	}

	vector<int> vals;
	for (int y : adj[x]) {
		vals.push_back(W[y] + dp[y][1] - dp[y][0]);
	}

	dp[x][1] = totc;
	if (vals.size() >= 2) {
		iter_swap(max_element(all(vals)), vals.begin());
		iter_swap(max_element(vals.begin() + 1, vals.end()), vals.begin() + 1);
		setmax(dp[x][1], vals[0] + vals[1] + totc);
	}

	dp[x][0] = totc;
	vals.clear();
	for (int y : adj[x]) {
		//debug("equis x = %d. Va %d\n", x, W[y] + W[x]);
		vals.push_back(W[y] + W[x] + dp[y][1] - dp[y][0]);
	}

	if (!vals.empty()) {
		setmax(dp[x][0], totc + *max_element(all(vals)));
	}

	//debug("dp[%d][%d] = %d, dp[%d][%d] = %d\n", x, 1, dp[x][1], x, 0, dp[x][0]);
}

int main() {
	scanf("%d", &N);
	for (int i = 1; i < N; i++) {
		int a, b, w;
		scanf("%d %d %d", &a, &b, &w);
		adj[a].push_back(b);
		adj[b].push_back(a);
		weight[a][b] = weight[b][a] = w;
	}

	dfs(1);
	printf("%d\n", dp[1][1]);
}

Compilation message

beads.cpp: In function 'int main()':
beads.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
beads.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &a, &b, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 16 ms 14564 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Incorrect 13 ms 14464 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 16 ms 14564 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Incorrect 13 ms 14464 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 16 ms 14564 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Incorrect 13 ms 14464 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 16 ms 14564 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Incorrect 13 ms 14464 KB Output isn't correct
7 Halted 0 ms 0 KB -