답안 #135057

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
135057 2019-07-23T15:08:07 Z ly20 구슬과 끈 (APIO14_beads) C++17
0 / 100
23 ms 20600 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN=212345;
vector<int> grafo[MAXN],peso[MAXN];
int dp[MAXN][5],marc[MAXN];
void dfs(int v,int p)
{
	marc[v]=1;
	if(v!=0 && grafo[v].size()==1)
	{
		dp[v][0]=0;dp[v][1]=p;dp[v][2]=0;
		return;
	}
	int d1=-1123456789,d2=-1123456789,tot=0;
	for(int i=0;i<grafo[v].size();i++)
	{
		int viz=grafo[v][i],ps=peso[v][i];
		if(marc[viz]==1)continue;
		dfs(viz,ps);
		if(dp[viz][1]-max(dp[viz][0],dp[viz][2])>d1)d1=dp[viz][1]-max(dp[viz][0],dp[viz][2]);
		if(d1>d2)swap(d1,d2);
		tot+=max(dp[viz][0],dp[viz][2]);
	}
	dp[v][0]=max(tot+d1+d2,tot);
	dp[v][2]=tot+d2+p;
	dp[v][1]=max(tot+d1+d2+p,tot+p);
}
int cn[MAXN];
int main()
{
	int n,in=0;
	scanf("%d",&n);
	for(int i=0;i<n-1;i++)
	{
		int a,b,p;cn[a]++;cn[b]++;
		if(cn[a]>cn[in])in=a;
		if(cn[b]>cn[in])in=b;
		scanf("%d %d %d",&a,&b,&p);a--;b--;
		grafo[a].push_back(b);grafo[b].push_back(a);
		peso[a].push_back(p);peso[b].push_back(p);
	}
	dfs(in,0);
	printf("%d\n",dp[in][0]);
	/*for(int i=0;i<n;i++)
	{
		printf("%d %d %d %d\n",i,dp[i][0],dp[i][1],dp[i][2]);
	}*/
	return 0;
}

Compilation message

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:15:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<grafo[v].size();i++)
              ~^~~~~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:32:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
beads.cpp:38:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&a,&b,&p);a--;b--;
   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 23 ms 20600 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 23 ms 20600 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 23 ms 20600 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 23 ms 20600 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -