답안 #35073

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
35073 2017-11-17T22:34:49 Z model_code Chase (CEOI17_chase) C++11
100 / 100
413 ms 170628 KB
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>

#define maxn 100001
#define maxv 101

using namespace std;
typedef long long ll;

const ll MOD = 1000000007LL;

int n,v;
vector<int> graf[maxn];
ll p[maxn];
ll gain[maxn];
ll cont[maxn][maxv];
ll arrive[maxn][maxv];
pair<ll,ll> bcont[maxv][2];
pair<ll,ll> bprev[maxv][2];
ll best;

void dp(int nd, int pr){
	for(int i=0;i<graf[nd].size();i++)if(graf[nd][i]!=pr)dp(graf[nd][i],nd);
	for(int i=0;i<=v;i++)cont[nd][i]=0;
	for(int i=0;i<=v;i++)for(int j=0;j<2;j++)bcont[i][j]=make_pair(0,-1);
	for(int i=0;i<graf[nd].size();i++){
		if(pr==-1)break;
		int next=graf[nd][i];
		if(next==pr)continue;
		for(int j=0;j<=v;j++)cont[nd][j]=max(cont[nd][j],cont[next][j]);
		for(int j=1;j<=v;j++)cont[nd][j]=max(cont[nd][j],cont[next][j-1]+gain[nd]-p[pr]);
	}
	for(int i=0;i<graf[nd].size();i++){
		int next=graf[nd][i];
		if(next==pr)continue;
		for(int j=0;j<=v;j++){
			ll adv = cont[next][j];
			if(adv>=bcont[j][0].first){
				bcont[j][1]=bcont[j][0];
				bcont[j][0]=make_pair(adv,next);
				continue;
			}
			if(adv>bcont[j][1].first)bcont[j][1]=make_pair(adv,next);
		}
	}
	for(int i=0;i<=v;i++)arrive[nd][i]=i==0?0:gain[nd];
	for(int i=0;i<=v;i++)for(int j=0;j<2;j++)bprev[i][j]=make_pair(i==0?0:gain[nd],nd);
	for(int i=0;i<graf[nd].size();i++){
		int prev=graf[nd][i];
		if(prev==pr)continue;
		for(int j=0;j<=v;j++)arrive[nd][j]=max(arrive[nd][j],arrive[prev][j]);
		for(int j=1;j<=v;j++)arrive[nd][j]=max(arrive[nd][j],arrive[prev][j-1]+gain[nd]-p[prev]);
		for(int j=0;j<=v;j++){
			ll adv=arrive[prev][j];
			if(j>0)adv=max(adv,arrive[prev][j-1]+gain[nd]-p[prev]);
			if(adv>=bprev[j][0].first){
				bprev[j][1]=bprev[j][0];
				bprev[j][0]=make_pair(adv,prev);
			} else if(adv>bprev[j][1].first)bprev[j][1]=make_pair(adv,prev);
		}
	}
	for(int i=0;i<=v;i++){
		ll pgain = 0;
		if(bcont[i][0].second!=bprev[v-i][0].second)pgain=bcont[i][0].first+bprev[v-i][0].first;
		else pgain = max(bcont[i][0].first+bprev[v-i][1].first,bcont[i][1].first+bprev[v-i][0].first);
		best=max(best,pgain);
	}
}

int main(){
	scanf("%d%d",&n,&v);
	for(int i=1;i<=n;i++)scanf("%lld",p+i);
	memset(gain,0,sizeof(gain));
	for(int i=0;i<n-1;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		graf[a].push_back(b);
		graf[b].push_back(a);
		gain[a]+=p[b];
		gain[b]+=p[a];
	}
	best=0;
	dp(1,-1);
	printf("%lld\n",best);
	return 0;
}

Compilation message

chase.cpp: In function 'void dp(int, int)':
chase.cpp:25:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<graf[nd].size();i++)if(graf[nd][i]!=pr)dp(graf[nd][i],nd);
               ^
chase.cpp:28:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<graf[nd].size();i++){
               ^
chase.cpp:35:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<graf[nd].size();i++){
               ^
chase.cpp:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<graf[nd].size();i++){
               ^
chase.cpp: In function 'int main()':
chase.cpp:73:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&v);
                     ^
chase.cpp:74:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;i++)scanf("%lld",p+i);
                                        ^
chase.cpp:78:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&a,&b);
                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 162896 KB Output is correct
2 Correct 0 ms 162896 KB Output is correct
3 Correct 0 ms 162896 KB Output is correct
4 Correct 3 ms 162896 KB Output is correct
5 Correct 0 ms 162896 KB Output is correct
6 Correct 0 ms 162896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 162896 KB Output is correct
2 Correct 0 ms 162896 KB Output is correct
3 Correct 0 ms 162896 KB Output is correct
4 Correct 3 ms 162896 KB Output is correct
5 Correct 0 ms 162896 KB Output is correct
6 Correct 0 ms 162896 KB Output is correct
7 Correct 0 ms 162896 KB Output is correct
8 Correct 0 ms 162896 KB Output is correct
9 Correct 0 ms 162896 KB Output is correct
10 Correct 3 ms 162896 KB Output is correct
11 Correct 3 ms 162896 KB Output is correct
12 Correct 0 ms 162896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 333 ms 170584 KB Output is correct
2 Correct 343 ms 170564 KB Output is correct
3 Correct 246 ms 166612 KB Output is correct
4 Correct 129 ms 166196 KB Output is correct
5 Correct 406 ms 166196 KB Output is correct
6 Correct 403 ms 166196 KB Output is correct
7 Correct 413 ms 166196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 162896 KB Output is correct
2 Correct 0 ms 162896 KB Output is correct
3 Correct 0 ms 162896 KB Output is correct
4 Correct 3 ms 162896 KB Output is correct
5 Correct 0 ms 162896 KB Output is correct
6 Correct 0 ms 162896 KB Output is correct
7 Correct 0 ms 162896 KB Output is correct
8 Correct 0 ms 162896 KB Output is correct
9 Correct 0 ms 162896 KB Output is correct
10 Correct 3 ms 162896 KB Output is correct
11 Correct 3 ms 162896 KB Output is correct
12 Correct 0 ms 162896 KB Output is correct
13 Correct 333 ms 170584 KB Output is correct
14 Correct 343 ms 170564 KB Output is correct
15 Correct 246 ms 166612 KB Output is correct
16 Correct 129 ms 166196 KB Output is correct
17 Correct 406 ms 166196 KB Output is correct
18 Correct 403 ms 166196 KB Output is correct
19 Correct 413 ms 166196 KB Output is correct
20 Correct 389 ms 166196 KB Output is correct
21 Correct 123 ms 166196 KB Output is correct
22 Correct 366 ms 166196 KB Output is correct
23 Correct 133 ms 166196 KB Output is correct
24 Correct 413 ms 166196 KB Output is correct
25 Correct 219 ms 166612 KB Output is correct
26 Correct 349 ms 170628 KB Output is correct
27 Correct 346 ms 170628 KB Output is correct