Submission #227775

#TimeUsernameProblemLanguageResultExecution timeMemory
227775MohamedAhmed04Dostavljač (COCI18_dostavljac)C++14
14 / 140
2092 ms2552 KiB
#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 (stderr)

dostavljac.cpp: In function 'int solve(int, int)':
dostavljac.cpp:31:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(idx == eul.size())
     ~~~~^~~~~~~~~~~~~
dostavljac.cpp:66:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(idx < eul.size()-1)
      ~~~~^~~~~~~~~~~~~~
dostavljac.cpp:74:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(idx < eul.size()-1)
     ~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...