Submission #511303

#TimeUsernameProblemLanguageResultExecution timeMemory
511303hollwo_pelwPaths (RMI21_paths)C++17
68 / 100
1062 ms15360 KiB
/* // is short or still long ??? hollwo_pelw's template(short) // Note : -Dhollwo_pelw_local */ #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/trie_policy.hpp> // #include <ext/rope> using namespace std; // using namespace __gnu_pbds; // using namespace __gnu_cxx; void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){ if (fopen(filein.c_str(), "r")){ freopen(filein.c_str(), "r", stdin); freopen(fileout.c_str(), "w", stdout); #ifdef hollwo_pelw_local freopen(fileerr.c_str(), "w", stderr); #endif } cin.tie(0), cout.tie(0) -> sync_with_stdio(0); } void Hollwo_Pelw(); signed main(){ #ifdef hollwo_pelw_local FAST_IO("input.inp", "output.out", "error.err"); auto start = chrono::steady_clock::now(); #else FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out"); #endif int testcases = 1; // cin >> testcases; for (int test = 1; test <= testcases; test++){ // cout << "Case #" << test << ": "; Hollwo_Pelw(); } #ifdef hollwo_pelw_local auto end = chrono::steady_clock::now(); cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl; #endif return 0; } const int N = 1e5 + 5; struct maxk { int k; int64_t val; multiset<int64_t> st; inline void __init__(int _k) { k = _k; } inline void add(int64_t v) { st.insert(v); } inline void del(int64_t v) { st.erase(st.find(v)); } inline int64_t get_val() { val = 0; auto it = st.end(); for (int q = k; q -- && it != st.begin(); ) val += *(-- it); return val; } } calc; int n, k; vector<pair<int, int>> adj[N]; int64_t up[N], dw[N], res[N]; void dfs_dw(int u, int p) { for (auto [v, w] : adj[u]) if (v != p) { dfs_dw(v, u); dw[u] = max(dw[u], dw[v] + w); } } void dfs_up(int u, int p) { int64_t vf = 0, vs = 0; int f = 0, s = 0; for (auto [v, w] : adj[u]) if (v != p) { int64_t vv = dw[v] + w; if (vf < vv) swap(f, v), swap(vf, vv); if (vs < vv) swap(s, v), swap(vs, vv); } for (auto [v, w] : adj[u]) if (v != p && v != f) { calc.add(dw[v] + w); } for (auto [v, w] : adj[u]) if (v != p) { up[v] = max(up[u], v == f ? vs : vf) + w; dfs_up(v, u); } } void reroot(int u, int p) { // cout << "solve " << u << '\n'; res[u] = calc.get_val(); for (auto [v, w] : adj[u]) if (v != p) { calc.add(dw[v]), calc.del(dw[v] + w); calc.add(up[v]), calc.del(up[v] - w); reroot(v, u); calc.add(dw[v] + w), calc.del(dw[v]); calc.add(up[v] - w), calc.del(up[v]); } } void Hollwo_Pelw() { cin >> n >> k; for (int i = 1, u, v, w; i < n; i++) { cin >> u >> v >> w; adj[u].emplace_back(v, w); adj[v].emplace_back(u, w); } calc.__init__(k); dfs_dw(1, 0); dfs_up(1, 0); calc.add(dw[1]); if (adj[1].size() == 1) calc.add(up[1]); reroot(1, 0); for (int i = 1; i <= n; i++) cout << res[i] << '\n'; }

Compilation message (stderr)

Main.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
Main.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen(filein.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen(fileout.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...