Submission #122298

# Submission time Handle Problem Language Result Execution time Memory
122298 2019-06-28T04:27:01 Z 임유진(#2991) Beads and wires (APIO14_beads) C++14
0 / 100
8 ms 5040 KB
#include<stdio.h>
#include<vector>

using namespace std;

#define MAXN 200005

typedef long long lint;
typedef pair<lint, lint> pll;

const lint INF=1ll<<50;
vector<pll> e[MAXN];
pll par[MAXN];
lint D[MAXN], P[MAXN];

void dfs(lint x){
	//printf("dfs(%d)\n", x);
	lint psum=0;
	lint mx[2]={-INF, -INF};
	for(auto a:e[x]) if(a.second!=par[x].second){
		par[a.second]=make_pair(a.first, x);
		dfs(a.second);
		psum+=P[a.second];
		lint t=D[a.second]-P[a.second]+a.first;
		if(t>mx[1]) mx[1]=t;
		if(mx[1]>mx[0]) swap(mx[0], mx[1]);
	}
	D[x]=psum+max(0ll, mx[0]+mx[1]);
	if(x!=1ll) P[x]=max(D[x], psum+mx[0]+par[x].first);
}

int main(){
	lint N;
	lint u[MAXN], v[MAXN], c[MAXN];

	scanf("%lld", &N);
	for(lint i=0ll; i<N-1; i++) scanf("%lld%lld%lld", u+i, v+i, c+i);

	for(lint i=0ll; i<N-1; i++){
		e[u[i]].push_back(make_pair(c[i], v[i]));
		e[v[i]].push_back(make_pair(c[i], u[i]));
	}
	dfs(1ll);

	//for(int i=1; i<=N; i++) printf("i=%d, D=%d, P=%d\n", i, D[i], P[i]);

	printf("%lld", D[1]);
	return 0;
}

Compilation message

beads.cpp: In function 'int main()':
beads.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld", &N);
  ~~~~~^~~~~~~~~~~~
beads.cpp:37:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(lint i=0ll; i<N-1; i++) scanf("%lld%lld%lld", u+i, v+i, c+i);
                              ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5040 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Correct 8 ms 4992 KB Output is correct
4 Correct 6 ms 5020 KB Output is correct
5 Correct 5 ms 4992 KB Output is correct
6 Incorrect 5 ms 4992 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5040 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Correct 8 ms 4992 KB Output is correct
4 Correct 6 ms 5020 KB Output is correct
5 Correct 5 ms 4992 KB Output is correct
6 Incorrect 5 ms 4992 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5040 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Correct 8 ms 4992 KB Output is correct
4 Correct 6 ms 5020 KB Output is correct
5 Correct 5 ms 4992 KB Output is correct
6 Incorrect 5 ms 4992 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5040 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Correct 8 ms 4992 KB Output is correct
4 Correct 6 ms 5020 KB Output is correct
5 Correct 5 ms 4992 KB Output is correct
6 Incorrect 5 ms 4992 KB Output isn't correct
7 Halted 0 ms 0 KB -