Submission #198669

# Submission time Handle Problem Language Result Execution time Memory
198669 2020-01-27T09:02:10 Z gs18081 Beads and wires (APIO14_beads) C++11
0 / 100
8 ms 5112 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pi;
typedef long long int ll;
	
const int MAXN = 202020;
const ll MAX = 2e9;

vector<pi> edge[MAXN];
ll plen[MAXN];
ll dp1[MAXN];
ll dp2[MAXN];
ll dp3[MAXN];
ll dp4[MAXN];
int N;

void dfs(int node,int par){
	ll mx2 = -MAX;
	ll mx4 = -MAX;
	vector<pi> vect3;
	int ccnt = 0;
	for(auto i : edge[node]){
		if(i.first == par) continue;
		ccnt++;
		plen[i.first] = i.second;
		dfs(i.first,node);
		dp1[node] += max(dp1[i.first] , dp2[i.first]);
		mx2 = max(mx2 , dp1[i.first] + plen[i.first] - max(dp1[i.first],dp2[i.first])); 
		mx4 = max(mx4 , dp3[i.first] + plen[i.first] - max(dp2[i.first],dp2[i.first])); 
		vect3.push_back(pi(dp1[i.first] + plen[i.first] - max(dp1[i.first],dp2[i.first]), i.first));
	}
	sort(vect3.begin(),vect3.end(),greater<pi>());
	dp2[node] = dp1[node] + mx2 + plen[node];
	dp4[node] = dp1[node] + mx4 + plen[node];
	dp3[node] = dp1[node];
	if(ccnt > 1){
		for(auto i : edge[node]){
			if(i.first == par) continue;
			int j;
			for(j = 0;j<vect3.size();j++){
				if(i.first != vect3[j].second) break;
			}
			dp3[node] = max(dp3[node] , dp1[node] + dp3[i.first] + plen[i.first] - max(dp1[i.first] , dp2[i.first]) + vect3[j].first);
		}
	}
//	printf("%d %lld %lld %lld %lld\n",node,dp1[node],dp2[node],dp3[node],dp4[node]);


	

}



int main(){
	scanf("%d",&N);
	for(int i=1;i<N;i++){
		int a,b,c;
		scanf("%d %d %d",&a,&b,&c);
		edge[a].push_back(pi(b,c));
		edge[b].push_back(pi(a,c));
	}
	dfs(1,1);
	printf("%lld\n",max(dp1[1],dp3[1]));
	return 0;
}

Compilation message

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(j = 0;j<vect3.size();j++){
              ~^~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
  ~~~~~^~~~~~~~~
beads.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&a,&b,&c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Incorrect 8 ms 5112 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Incorrect 8 ms 5112 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Incorrect 8 ms 5112 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Incorrect 8 ms 5112 KB Output isn't correct
4 Halted 0 ms 0 KB -