답안 #650167

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
650167 2022-10-12T16:24:52 Z inksamurai Dostavljač (COCI18_dostavljac) C++17
14 / 140
58 ms 15588 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3yqVz8E ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

signed main(){
_3yqVz8E;
	int n,m;
	cin>>n>>m;
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	vec(vi) adj(n);
	rep(i,n-1){
		int u,v;
		cin>>u>>v;
		u-=1,v-=1;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	vi par(n,-1);
	auto dfs=[&](auto self,int v,int _par)->void{
		par[v]=_par;
		for(auto u:adj[v]){
			if(u!=_par){
				self(self,u,v);
			}
		}
	};
	dfs(dfs,0,-1);
	vec(vi) leb(n,vi(n,-1));
	rep(v,n){
		adj[v].pb(v);
		int si=sz(adj[v]);
		rep(j,si){
			leb[v][adj[v][j]]=j;
		}
	}
	const int inf=1e9;
	vec(vec(vi)) dp(m+1,vec(vi)(n));
	rep(i,m+1){
		rep(v,n){
			int si=sz(adj[v]);
			rep(j,si){
				dp[i][v].pb(-inf);
			}
		}
	}
	auto affine=[&](int i,int v,int u,int val)->void{
		if(val<0) return;
		int j=leb[v][u];
		// print(j);
		dp[i][v][j]=max(dp[i][v][j],val);
	};
	// don't take root
	affine(0,0,0,0);
	affine(1,0,0,a[0]);
	rep(i,m){
		rep(v,n){
			int _par=par[v];
			int now=dp[i][v][leb[v][v]];
			if(_par!=-1){
				affine(i+1,v,v,dp[i][v][leb[v][_par]]+a[v]);
				now=max(now,dp[i][v][leb[v][_par]]);
			}
			int si=sz(adj[v]);
			rep(j,si){
				int u=adj[v][j];
				if(u==_par or u==v) continue;
				affine(i+1,u,v,now);
				now=max(now,dp[i][v][j]);
			}
			if(_par!=-1){
				affine(i+1,_par,v,now);
			}
		}
		// print("time is = ",i);
		// rep(v,n){
		// 	print("node is = ",v);
		// 	int si=sz(adj[v]);
		// 	rep(j,si){
		// 		int u=adj[v][j];
		// 		cout<<u<<" ";
		// 	}
		// 	print();
		// 	rep(j,si){
		// 		cout<<dp[i][v][j]<<" ";
		// 	}
		// 	print();
		// }
		// print(".....");
	}
	int ans=0;
	rep(i,m+1){
		rep(v,n){
			int si=sz(adj[v]);
			rep(j,si){
				ans=max(ans,dp[i][v][j]);
			}
		}
	}
	print(ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 1364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1108 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 3916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 47 ms 10068 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 58 ms 15588 KB Output isn't correct
2 Halted 0 ms 0 KB -