Submission #884920

#TimeUsernameProblemLanguageResultExecution timeMemory
884920HossamHero7Parkovi (COCI22_parkovi)C++14
30 / 110
1769 ms43528 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' const int N = 2e5 + 5; vector<pair<int,ll>> adj[N]; pair<int,ll> P[N]; vector<pair<int,int>> leaves; void dfs(int node,int par,int dep){ if(adj[node].size() == 1 && node != 1) leaves.push_back({dep,node}); for(auto [ch,c] : adj[node]){ if(ch == par) continue; P[ch] = {node,c}; dfs(ch,node,dep+1); } } void solve(){ int n,k; cin>>n>>k; for(int i=0;i<n-1;i++){ int a,b,c; cin>>a>>b>>c; adj[a].push_back({b,c}); adj[b].push_back({a,c}); } dfs(1,0,0); ll l = 1; ll r = 1e18; ll ans = 0; vector<int> v; while(l <= r){ ll md = l + r >> 1; priority_queue<pair<int,int>> q; vector<bool> vis(n+1); for(auto i : leaves) q.push(i) , vis[i.second] = 1; vector<ll> cost(n+1); vector<ll> covered(n+1,1e18); vector<int> tmp; while(q.size()){ auto [dep,node] = q.top(); q.pop(); auto [par,c] = P[node]; if(covered[node] <= md){ //cout<<"A "<<node<<' '<<covered[node]<<' '<<cost[node]<<endl; covered[par] = min(covered[par] , covered[node] + c); if(!vis[par] && par) q.push({dep-1,par}) , vis[par] = 1; } else { if(covered[par] <= md){ if(covered[par] + cost[node] + c <= md) { if(!vis[par] && par) q.push({dep-1,par}) , vis[par] = 1; } else { tmp.push_back(node); if(!vis[par] && par) q.push({dep-1,par}) , vis[par] = 1; } } else { if(cost[node] + c <= md){ //cout<<"B1 "<<node<<' '<<covered[node]<<' '<<cost[node]<<endl; cost[par] = max(cost[par],cost[node] + c); if(!vis[par] && par) q.push({dep-1,par}) , vis[par] = 1; } else { tmp.push_back(node); covered[par] = min(covered[par] , c); if(!vis[par] && par) q.push({dep-1,par}) , vis[par] = 1; } } } } if((tmp.empty() || tmp.back() != 1) && covered[1] > md) tmp.push_back(1); if(tmp.size() <= k){ ans = md; v = tmp; r = md - 1; } else l = md + 1; } cout<<ans<<endl; vector<bool> vis(n+1); for(auto i : v) vis[i] = 1; for(int i=1;i<=n&&v.size()!=k;i++){ if(!vis[i]) v.push_back(i); } for(auto i : v) cout<<i<<' '; cout<<endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } return 0; }

Compilation message (stderr)

Main.cpp: In function 'void dfs(int, int, int)':
Main.cpp:11:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   11 |     for(auto [ch,c] : adj[node]){
      |              ^
Main.cpp: In function 'void solve()':
Main.cpp:32:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |         ll md = l + r >> 1;
      |                 ~~^~~
Main.cpp:40:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   40 |             auto [dep,node] = q.top();       q.pop();
      |                  ^
Main.cpp:41:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   41 |             auto [par,c] = P[node];
      |                  ^
Main.cpp:72:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   72 |         if(tmp.size() <= k){
      |            ~~~~~~~~~~~^~~~
Main.cpp:82:31: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   82 |     for(int i=1;i<=n&&v.size()!=k;i++){
      |                       ~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...