답안 #439307

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
439307 2021-06-29T14:38:19 Z keta_tsimakuridze 구슬과 끈 (APIO14_beads) C++14
28 / 100
1000 ms 1100 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,mx[N],x[N],leaf[N];
vector<pair<int,int> > V[N];
void dfs(int u,int p) {
	leaf[u] = 0;
	dp[u][0] = dp[u][1]  = 0;
	dp[u][0] = -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][0] + V[u][i].s,dp[v][1]),x =max(dp[v][0] + V[u][i].s,dp[v][1]);
		dp[u][0] = max(dp[u][0], -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 ans = 0;
	for(int i=1;i<=n;i++){ 
		dfs(i,0);
		ans = max(ans,dp[i][1]);
	}
	//
//	dfs2(1,0); 
	cout<<ans;
}

Compilation message

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:13: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]
   13 |  for(int i=0;i<V[u].size();i++) {
      |              ~^~~~~~~~~~~~
beads.cpp: At global scope:
beads.cpp:25:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   25 |  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 512 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 512 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 2 ms 572 KB Output is correct
17 Correct 2 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 2 ms 580 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 2 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 512 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 2 ms 572 KB Output is correct
17 Correct 2 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 2 ms 580 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 778 ms 856 KB Output is correct
24 Correct 749 ms 856 KB Output is correct
25 Correct 791 ms 856 KB Output is correct
26 Execution timed out 1085 ms 1100 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 512 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 2 ms 572 KB Output is correct
17 Correct 2 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 2 ms 580 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 778 ms 856 KB Output is correct
24 Correct 749 ms 856 KB Output is correct
25 Correct 791 ms 856 KB Output is correct
26 Execution timed out 1085 ms 1100 KB Time limit exceeded
27 Halted 0 ms 0 KB -