답안 #96169

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
96169 2019-02-06T16:57:52 Z Retro3014 구슬과 끈 (APIO14_beads) C++17
13 / 100
6 ms 5120 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

#define MAX_N 200000
#define INF 2000000000LL
using namespace std;
typedef long long ll;

int N;
int ans;
struct S{
	S (int idx, ll data) : idx(idx), data(data) {}
	int idx;ll data;
};
vector<S> gp[MAX_N+1];
vector<S> p;
ll dp[MAX_N+1][4];

void dfs(int x){
	for(int i=0; i<gp[x].size(); i++){
		if(gp[x][i].idx!=p[x].idx){
			p[gp[x][i].idx].idx = x;
			p[gp[x][i].idx].data = gp[x][i].data;
			dfs(gp[x][i].idx);
		}
	}
	ll sum = 0;
	for(int i=0; i<gp[x].size(); i++){
		if(gp[x][i].idx==p[x].idx)	continue;
		int now = gp[x][i].idx;
		dp[x][0] += max(dp[now][0], max(dp[now][1], dp[now][2]));
		dp[x][1] += max(dp[now][0], max(dp[now][1], dp[now][3]));
		sum += max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]));
	}
	ll mx1 = 0, mx2 = 0;
	dp[x][2] = dp[x][3] = -INF;
	bool tf = true;
	for(int i=0; i<gp[x].size(); i++){
		if(gp[x][i].idx==p[x].idx)	continue;
		if(tf){
			tf = false;
			mx1 = dp[gp[x][i].idx][0] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]));
			mx2 = dp[gp[x][i].idx][1] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]));
		}else{
			dp[x][1] = max(dp[x][1], sum + mx1 + gp[x][i].data + dp[gp[x][i].idx][0] - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
			dp[x][1] = max(dp[x][1], sum + mx1 + dp[gp[x][i].idx][1] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
			dp[x][1] = max(dp[x][1], sum + mx2 + gp[x][i].data + dp[gp[x][i].idx][0] - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
			mx1 = max(mx1, dp[gp[x][i].idx][0] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
			mx2 = max(mx2, dp[gp[x][i].idx][1] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
		}
		dp[x][2] = max(dp[x][2], dp[gp[x][i].idx][1] + sum - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])) + gp[x][i].data + p[x].data);
		dp[x][3] = max(dp[x][3], dp[gp[x][i].idx][0] + sum - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])) + gp[x][i].data + p[x].data);
		dp[x][2] = max(dp[x][2], dp[x][3]);
	}
	return;
}


int main(){
	scanf("%d", &N);
	for(int i=0; i<=N; i++)	p.push_back({0, 0});
	for(int i=0; i<N-1; i++){
		int a, b;ll c;
		scanf("%d%d%lld", &a, &b, &c);
		gp[a].push_back({b, c}); gp[b].push_back({a, c});
	}
	dfs(1);
	printf("%lld", max(dp[1][0], dp[1][1]));
	return 0;
}

Compilation message

beads.cpp: In function 'void dfs(int)':
beads.cpp:22:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp:30:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp:40:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
beads.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%lld", &a, &b, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 5 ms 4992 KB Output is correct
3 Correct 5 ms 4992 KB Output is correct
4 Correct 6 ms 4992 KB Output is correct
5 Correct 6 ms 4992 KB Output is correct
6 Correct 6 ms 5120 KB Output is correct
7 Correct 5 ms 4992 KB Output is correct
8 Correct 5 ms 4992 KB Output is correct
9 Correct 5 ms 4992 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 6 ms 4992 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 5 ms 4992 KB Output is correct
3 Correct 5 ms 4992 KB Output is correct
4 Correct 6 ms 4992 KB Output is correct
5 Correct 6 ms 4992 KB Output is correct
6 Correct 6 ms 5120 KB Output is correct
7 Correct 5 ms 4992 KB Output is correct
8 Correct 5 ms 4992 KB Output is correct
9 Correct 5 ms 4992 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 6 ms 4992 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Incorrect 6 ms 4992 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 5 ms 4992 KB Output is correct
3 Correct 5 ms 4992 KB Output is correct
4 Correct 6 ms 4992 KB Output is correct
5 Correct 6 ms 4992 KB Output is correct
6 Correct 6 ms 5120 KB Output is correct
7 Correct 5 ms 4992 KB Output is correct
8 Correct 5 ms 4992 KB Output is correct
9 Correct 5 ms 4992 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 6 ms 4992 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Incorrect 6 ms 4992 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 5 ms 4992 KB Output is correct
3 Correct 5 ms 4992 KB Output is correct
4 Correct 6 ms 4992 KB Output is correct
5 Correct 6 ms 4992 KB Output is correct
6 Correct 6 ms 5120 KB Output is correct
7 Correct 5 ms 4992 KB Output is correct
8 Correct 5 ms 4992 KB Output is correct
9 Correct 5 ms 4992 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 6 ms 4992 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Incorrect 6 ms 4992 KB Output isn't correct
14 Halted 0 ms 0 KB -