Submission #227831

# Submission time Handle Problem Language Result Execution time Memory
227831 2020-04-29T01:38:16 Z MohamedAhmed04 Dostavljač (COCI18_dostavljac) C++17
14 / 140
2000 ms 2556 KB
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
 
using namespace std ;
 
const int MAX = 500 + 10 ;
 
int arr[MAX] , in[MAX] , out[MAX] , nxt[MAX+MAX] ;
int dp[MAX+MAX][MAX] ;
int n , m ;
vector< vector<int> >adj(MAX) ;
vector<int>eul ;
 
void dfs(int node , int par)
{
	in[node] = eul.size() ;
	eul.push_back(node) ;
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		dfs(child , node) ;
		nxt[in[child]] = eul.size() ;
		eul.push_back(node) ;
	}
	out[node] = eul.size()-1 ;
	return ;
}
 
int solve(int idx, int rem)
{
	if(idx == eul.size())
		return -1e9 ;
	if(rem == 0)
	{
		if(eul[idx] == 1)
			return 0 ;
		else
			return -1e9 ;
	}
	int &ret = dp[idx][rem] ;
	if(ret != -1)
		return ret ;
	int node = eul[idx] ;
	//fourth choice
	ret = -1e9 ;
	if(node == 1)
		ret = 0 ;
	/* 4 choices
	First choice is to buy and stop
	Second choice is to buy and continue
	third choice is to continue and not buying
	Fourth choice is stop*/
	if(in[node] == idx)
	{
		// first choice
		if(node == 1)
			ret = max(ret , arr[node]) ;
		// second choice
		/* 2 choices
		Go to the next node
		Go to the next subtree if next node is still my child
		*/
		if(rem >= 2)
			ret = max(ret , solve(idx+1 , rem-2) + arr[node]) ;
		int to = -1 ;
		if(idx < eul.size()-1)
			to = eul[idx+1] ;
		if(to != -1 && in[to] >= in[node] && out[to] <= out[node])
			ret = max(ret , solve(nxt[idx+1] , rem-1) + arr[node]) ;
	}
	//third choice
	ret = max(ret , solve(idx+1 , rem-1)) ;
	int to = -1 ;
	if(idx < eul.size()-1)
		to = eul[idx+1] ;
	if(to != -1 && in[to] >= in[node] && out[to] <= out[node])
		ret = max(ret , solve(nxt[idx+1] , rem)) ;
	return ret ;
}
 
int main()
{
	memset(dp , -1 , sizeof(dp)) ;
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>m ;
	for(int i = 1 ; i <= n ; ++i)
		cin>>arr[i] ;
	for(int i = 0 ; i < n-1 ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		adj[x].push_back(y) ;
		adj[y].push_back(x) ;
	}
	int ans = 0 ;
	for(int i = 1 ; i <= n ; ++i)
	{
		eul.clear() ;
		dfs(i , -1) ;
		memset(dp , -1 , sizeof(dp)) ;
		ans = max(ans , solve(0 , m)) ;
	}
	return cout<<ans<<"\n" , 0 ;
}		

Compilation message

dostavljac.cpp: In function 'int solve(int, int)':
dostavljac.cpp:34:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(idx == eul.size())
     ~~~~^~~~~~~~~~~~~
dostavljac.cpp:69:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(idx < eul.size()-1)
      ~~~~^~~~~~~~~~~~~~
dostavljac.cpp:77:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(idx < eul.size()-1)
     ~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 2432 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 2432 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 2432 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 60 ms 2552 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 73 ms 2432 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 447 ms 2504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 650 ms 2472 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2089 ms 2556 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2091 ms 2432 KB Time limit exceeded
2 Halted 0 ms 0 KB -