Submission #858407

#TimeUsernameProblemLanguageResultExecution timeMemory
858407thangdz2k7Paths (RMI21_paths)C++17
100 / 100
147 ms18772 KiB
#include <bits/stdc++.h> #define int long long #define ii pair <int, int> #define F first #define S second using namespace std; const int mod = 1e9 + 7; const int N = 1e5 + 5; int n, k, dem; vector <ii> c[N]; long long ans[N], dp1[N], dp2[N], a = 0; multiset <long long> s, Maxk; void add(long long val){ //cout << 1 << ' ' << val << '\n'; if (val >= *Maxk.begin()) Maxk.insert(val), a += val; else s.insert(val); } void adv(){ while (Maxk.size() < k and s.size() >= 1){ a += *s.rbegin(); Maxk.insert(*s.rbegin()); s.erase(s.lower_bound(*s.rbegin())); } while (Maxk.size() > k){ a -= *Maxk.begin(); s.insert(*Maxk.begin()); Maxk.erase(Maxk.begin()); } } void del(long long val){ //cout << 0 << ' ' << val << '\n'; if (Maxk.count(val)) Maxk.erase(Maxk.lower_bound(val)), a -= val; else s.erase(s.lower_bound(val)); } void dfs(int u, int pa){ //add(0, ans[u]); dp1[u] = 0, dp2[u] = 0; for (ii v : c[u]){ if (v.F == pa) continue; dfs(v.F, u); if (dp1[u] < dp1[v.F] + v.S) dp2[u] = dp1[u], dp1[u] = dp1[v.F] + v.S; else dp2[u] = max(dp2[u], dp1[v.F] + v.S); } bool used = false; for (ii v : c[u]){ if (v.F == pa) continue; if (used or dp1[v.F] + v.S < dp1[u] or u == 1) add(dp1[v.F] + v.S); else used = true; } } void get(int u, int pa){ adv(); ans[u] = a; for (ii v : c[u]){ if (v.F == pa) continue; //cout << v.F << endl; int val1 = dp1[v.F] + v.S; int val2 = dp1[v.F]; del(dp1[v.F] + v.S); add(dp1[v.F]); //if (v.F == 4) cout << -111111 << endl; long long val = dp1[u]; if (val == dp1[v.F] + v.S) val = dp2[u]; add(val + v.S); del(val); //cout << val << endl; if (dp1[v.F] < val + v.S){ dp2[v.F] = dp1[v.F]; dp1[v.F] = val + v.S; } else dp2[v.F] = max(dp2[v.F], val + v.S); get(v.F, u); long long kk = 0; add(val1); del(val2); add(val); del(val + v.S); } } void solve(){ cin >> n >> k; for (int i = 1; i <= n - 1; ++ i){ int u, v, w; cin >> u >> v >> w; c[u].push_back(ii(v, w)); c[v].push_back(ii(u, w)); } dfs(1, 0); get(1, 0); if (k == 1) for (int i = 1; i <= n; ++ i) cout << dp1[i] << '\n'; else for (int i = 1; i <= n; ++ i) cout << ans[i] << '\n'; } signed main(){ //freopen("code.inp", "r", stdin); //freopen("code.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t --) solve(); return 0; }

Compilation message (stderr)

Main.cpp: In function 'void adv()':
Main.cpp:24:24: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   24 |     while (Maxk.size() < k and s.size() >= 1){
      |            ~~~~~~~~~~~~^~~
Main.cpp:29:24: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   29 |     while (Maxk.size() > k){
      |            ~~~~~~~~~~~~^~~
Main.cpp: In function 'void get(long long int, long long int)':
Main.cpp:83:19: warning: unused variable 'kk' [-Wunused-variable]
   83 |         long long kk = 0;
      |                   ^~
#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...