답안 #439312

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
439312 2021-06-29T14:42:55 Z keta_tsimakuridze 구슬과 끈 (APIO14_beads) C++14
57 / 100
10 ms 1484 KB
#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=1e4+5,mod=1e9+7;
int dp[N][2],n,x[N],leaf[N],ans;
vector<pair<int,int> > V[N];
pair<int,int> mx[N];
void dfs2(int u,int p) {
	ans = max(ans,dp[u][1]); 
	for(int i=0;i<V[u].size();i++) {
		int v = V[u][i].f;
		if(v==p) continue;
		
		int x = 0,bef0 = dp[u][0],bef1 = dp[u][1];
		if(!leaf[v]) x = max(dp[v][0] + V[u][i].s,dp[v][1]);
		dp[u][1] -= x;
		
		dp[u][0] = mx[u].f;
		if(mx[u].f == -x+dp[v][1]+V[u][i].s) dp[u][0] = mx[u].s;
		dp[u][0] += dp[u][1];
		x = max(dp[u][1],dp[u][0] + V[u][i].s);
		dp[v][1] += max(dp[u][1],dp[u][0] + V[u][i].s);
		dp[v][0] = max(dp[v][0],-x + V[u][i].s + dp[u][1]);
		if(-x +dp[u][1]+V[u][i].s > mx[v].f) {
			swap(mx[v].f,mx[v].s);
			mx[v].f = -x +dp[u][1]+V[u][i].s;
		}
		else mx[v].s = max(mx[v].s,-x +dp[u][1]+V[u][i].s);
		dfs2(v,u);
		dp[u][0] = bef0;
		dp[u][1] = bef1;
		
	}
}
void dfs(int u,int p) {
	leaf[u] = 0;
	dp[u][0] = dp[u][1]  = 0;
	dp[u][0] = -1e8;
	mx[u] = {-1e8,-1e8};
	for(int i=0;i<V[u].size();i++) {
		int v = V[u][i].f;
		if(v==p) continue;
		dfs(v,u);
		int x = 0;
		if(!leaf[v]) dp[u][1] += max(dp[v][1],dp[v][0] + V[u][i].s),x = max(dp[v][1],dp[v][0] + V[u][i].s);
		dp[u][0] = max(dp[u][0], -x +dp[v][1]+V[u][i].s);
		if(-x +dp[v][1]+V[u][i].s > mx[u].f) {
			swap(mx[u].f,mx[u].s);
			mx[u].f = -x +dp[v][1]+V[u][i].s;
		}
		else mx[u].s = max(mx[u].s,-x +dp[v][1]+V[u][i].s);
	}
	dp[u][0] += dp[u][1];

	if(V[u].size()==1 && p) leaf[u] = 1;
}
 main(){
   ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=2;i<=n;i++){
		int u,v,w;
		cin>>u>>v>>w;
		V[u].push_back({v,w});
		V[v].push_back({u,w});
	}
	int u = 1;
	for(int i=1;i<=n;i++) if(V[i].size()!=1){
		u = i; break;
	}
	dfs(u,0);
	//
	dfs2(u,0); 
	cout<<ans;
}

Compilation message

beads.cpp: In function 'void dfs2(int, int)':
beads.cpp:11:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |  for(int i=0;i<V[u].size();i++) {
      |              ~^~~~~~~~~~~~
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:41:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for(int i=0;i<V[u].size();i++) {
      |              ~^~~~~~~~~~~~
beads.cpp: At global scope:
beads.cpp:58:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   58 |  main(){
      |  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 4 ms 844 KB Output is correct
24 Correct 4 ms 844 KB Output is correct
25 Correct 3 ms 844 KB Output is correct
26 Correct 10 ms 1100 KB Output is correct
27 Correct 6 ms 1268 KB Output is correct
28 Correct 8 ms 1228 KB Output is correct
29 Correct 6 ms 1360 KB Output is correct
30 Correct 7 ms 1228 KB Output is correct
31 Correct 6 ms 1484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 4 ms 844 KB Output is correct
24 Correct 4 ms 844 KB Output is correct
25 Correct 3 ms 844 KB Output is correct
26 Correct 10 ms 1100 KB Output is correct
27 Correct 6 ms 1268 KB Output is correct
28 Correct 8 ms 1228 KB Output is correct
29 Correct 6 ms 1360 KB Output is correct
30 Correct 7 ms 1228 KB Output is correct
31 Correct 6 ms 1484 KB Output is correct
32 Runtime error 1 ms 972 KB Execution killed with signal 11
33 Halted 0 ms 0 KB -