제출 #1124715

#제출 시각아이디문제언어결과실행 시간메모리
1124715Zero_OPPaths (RMI21_paths)C++17
8 / 100
715 ms589824 KiB
#include <bits/stdc++.h> using namespace std; #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define sz(v) (int)v.size() #define dbg(x) "[" #x " = " << (x) << "]" #define all(v) begin(v), end(v) #define compact(v) v.erase(unique(all(v)), end(v)) #define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } template<typename T> bool minimize(T& a, const T& b){ if(a > b){ return a = b, true; } return false; } template<typename T> bool maximize(T& a, const T& b){ if(a < b){ return a = b, true; } return false; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); using ll = long long; using ld = long double; using ull = unsigned long long; using vi = vector<int>; using vl = vector<ll>; const int MAX = 1e5 + 5; int N, K; vector<pair<int, int>> adj[MAX]; namespace subtask1{ bool check(){ return N <= 18; } int par[20], par_e[20]; bool vis[20]; vector<int> masks; void dfs(int u, int p){ par[u] = p; for(auto [v, w] : adj[u]) if(v != p){ par_e[v] = w; dfs(v, u); } } long long calc_up(int u){ long long s = 0; while(!vis[u]){ vis[u] = true; s += par_e[u]; u = par[u]; } return s; } long long solve_root(int rt){ par_e[rt] = 0; dfs(rt, -1); long long ans = 0; for(auto msk : masks){ fill(vis, vis + N, false); vis[rt] = 0; long long cur = 0; for(int i = 0; i < N; ++i){ if(msk >> i & 1){ cur += calc_up(i); } } ans = max(ans, cur); } return ans; } long long ans[20]; void solve(){ for(int mask = (1 << K) - 1; mask < (1 << N); ++mask){ if(__builtin_popcount(mask) == K){ masks.push_back(mask); } } for(int r = 0; r < N; ++r){ ans[r] = solve_root(r); } for(int r = 0; r < N; ++r) cout << ans[r] << '\n'; } } namespace subtask5{ bool check(){ return K == 1; } long long dp_down[MAX], dp_up[MAX]; void dfs_down(int u, int p){ for(auto [v, w] : adj[u]) if(v != p){ dfs_down(v, u); maximize(dp_down[u], dp_down[v] + w); } } void dfs_up(int u, int p){ vector<long long> pref, suff; for(auto [v, w] : adj[u]) if(v != p){ pref.push_back(dp_down[v] + w); suff.push_back(dp_down[v] + w); } for(int i = 1; i < sz(pref); ++i) pref[i] = max(pref[i - 1], pref[i]); for(int i = sz(suff) - 2; i >= 0; --i) suff[i] = max(suff[i + 1], suff[i]); int i = 0; for(auto [v, w] : adj[u]){ dp_up[v] = dp_up[u]; if(i > 0) maximize(dp_up[v], pref[i - 1] + w); if(i + 1 < sz(suff)) maximize(dp_up[v], suff[i + 1] + w); dfs_up(v, u); } } void solve(){ dfs_down(0, -1); dfs_up(0, -1); for(int i = 0; i < N; ++i){ cout << max(dp_down[i], dp_up[i]) << '\n'; } } } namespace subtask234{ bool check(){ return N <= 2000; } pair<long long, int> dp[MAX]; long long ans[2005], sum; priority_queue<long long, vector<long long>, greater<long long>> min_pq; void add(long long x){ if((int)min_pq.size() == K){ if(min_pq.top() < x){ sum -= min_pq.top(); min_pq.pop(); sum += x; min_pq.push(x); } } else{ sum += x; min_pq.push(x); } } void solve(int u, int p){ dp[u] = {0, u}; for(auto [v, w] : adj[u]) if(v != p){ solve(v, u); if(dp[u] < make_pair(dp[v].first + w, dp[v].second)){ dp[u] = make_pair(dp[v].first + w, dp[v].second); } } for(auto [v, w] : adj[u]) if(v != p){ if(dp[v].second != dp[u].second){ add(dp[v].first); } } if(p == -1) add(dp[u].first); } void solve(){ for(int r = 0; r < N; ++r){ solve(r, -1); ans[r] = sum; sum = 0; priority_queue<long long, vector<long long>, greater<long long>>().swap(min_pq); } for(int i = 0; i < N; ++i){ cout << ans[i] << '\n'; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); file("B"); cin >> N >> K; for(int i = 1; i < N; ++i){ int u, v, w; cin >> u >> v >> w; --u, --v; adj[u].emplace_back(v, w); adj[v].emplace_back(u, w); } if(subtask1::check()) return subtask1::solve(), 0; if(subtask234::check()) return subtask234::solve(), 0; if(subtask5::check()) return subtask5::solve(), 0; return 0; }

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

Main.cpp: In function 'int main()':
Main.cpp:10:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 | #define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:208:5: note: in expansion of macro 'file'
  208 |     file("B");
      |     ^~~~
Main.cpp:10:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 | #define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:208:5: note: in expansion of macro 'file'
  208 |     file("B");
      |     ^~~~
#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...