제출 #525164

#제출 시각아이디문제언어결과실행 시간메모리
525164Yazan_AlattarChase (CEOI17_chase)C++14
100 / 100
529 ms506864 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define F first #define S second #define pb push_back #define endl "\n" #define all(x) x.begin(), x.end() const int M = 100007; const ll inf = 1e9; const ll mod = 998244353; const double pi = acos(-1); const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; vector <int> adj[M]; pair <ll,ll> mx[M][105][2]; int n, v, a[M], who[M][105][2]; ll dp[M][105], ans; void dfs(int node, int p) { ll sum = 0; for(auto i : adj[node]) if(i != p) sum += a[i]; for(auto i : adj[node]) if(i != p){ dfs(i, node); for(int j = 1; j <= v; ++j) dp[node][j] = max(dp[node][j], max(dp[i][j], dp[i][j - 1] + sum)); } return; } void reroot(int node, int p) { ll sum = 0; for(auto i : adj[node]) sum += a[i]; for(auto i : adj[node]) for(int j = 1; j <= v; ++j) dp[node][j] = max(dp[node][j], max(dp[i][j], dp[i][j - 1] + sum)); ans = max(ans, dp[node][v]); for(auto i : adj[node]){ for(int j = 1; j <= v; ++j){ for(int k = 0; k < 2; ++k){ if(dp[i][j - k] > mx[node][j][k].F){ mx[node][j][k].S = mx[node][j][k].F; mx[node][j][k].F = dp[i][j - k]; who[node][j][k] = i; } else if(dp[i][j - k] > mx[node][j][k].S) mx[node][j][k].S = dp[i][j - k]; } } } for(auto i : adj[node]) if(i != p){ for(int j = 1; j <= v; ++j){ ll x[2] = {0}; for(int k = 0; k < 2; ++k){ if(who[node][j][k] == i) x[k] = mx[node][j][k].S; else x[k] = mx[node][j][k].F; } dp[node][j] = max(x[0], x[1] + sum - a[i]); } reroot(i, node); } return; } int main() { scanf("%d%d", &n, &v); for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); for(int i = 1; i < n; ++i){ int x, y; scanf("%d%d", &x, &y); adj[x].pb(y); adj[y].pb(x); } dfs(1, 0); reroot(1, 0); printf("%lld\n", ans); return 0; }

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

chase.cpp: In function 'int main()':
chase.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |  scanf("%d%d", &n, &v);
      |  ~~~~~^~~~~~~~~~~~~~~~
chase.cpp:69:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |  for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
      |                              ~~~~~^~~~~~~~~~~~~
chase.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |   scanf("%d%d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...