제출 #227831

#제출 시각아이디문제언어결과실행 시간메모리
227831MohamedAhmed04Dostavljač (COCI18_dostavljac)C++17
14 / 140
2091 ms2556 KiB
#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 ; }

컴파일 시 표준 에러 (stderr) 메시지

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 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...