Submission #1173000

#TimeUsernameProblemLanguageResultExecution timeMemory
1173000InvMODParkovi (COCI22_parkovi)C++20
110 / 110
871 ms44576 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define gcd __gcd #define sz(v) (int) v.size() #define pb push_back #define pi pair<int,int> #define all(v) (v).begin(), (v).end() #define compact(v) (v).erase(unique(all(v)), (v).end()) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define REV(i, a, b) for(int i = (a); i >= (b); i--) #define dbg(x) "[" #x " = " << (x) << "]" ///#define int long long using ll = long long; using ld = long double; using ull = unsigned long long; template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;} template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;} const int N = 2e5+5; const ll MOD = 1e9+7; const ll INF = 1e18; struct Edge{ int u,v,w; Edge(int u = 0, int v = 0, int w = 0): u(u), v(v), w(w) {} }; void solve() { int n,k; cin >> n >> k; vector<Edge> E(n); for(int i = 1; i < n; i++){ int u,v,w; cin >> u >> v >> w; E[i] = Edge(u, v, w); } vector<vector<int>> adj(n + 1, vector<int>()); for(int i = 1; i < n; i++){ int u = E[i].u, v = E[i].v; adj[u].push_back(i); adj[v].push_back(i); } vector<bool> choosed(n + 1, false); auto check = [&](ll MxDist) -> bool{ int cntPark = 0; vector<ll> noPark(n + 1), nearPark(n + 1, INF); // noPark[x]: farthest dist inside subtree x that has no park / can't go to any park // nearPark[x]: nearest park inside subtree x and nearest to x function<void(int,int)> calc_dp = [&](int x, int idE) -> void{ choosed[x] = false; for(int id : adj[x]){ int v = E[id].u ^ E[id].v ^ x; ll w = E[id].w; if(id != idE){ calc_dp(v, id); nearPark[x] = min(nearPark[x], nearPark[v] + w); } } if(nearPark[x] + noPark[x] > MxDist){ // we will have to choose park from parent else we will choose inside our subtree if(idE < 0){ // no parent to choose so we have to choose x choosed[x] = true; nearPark[x] = 0; } else{ int p = E[idE].u ^ E[idE].v ^ x; if(noPark[x] + 1ll * E[idE].w <= MxDist){ // we can keep noPark to parent noPark[p] = max(noPark[p], noPark[x] + 1ll * E[idE].w); } else{ // we can't keep it so we have to choose it choosed[x] = true; nearPark[x] = 0; } } } cntPark += choosed[x]; return; }; calc_dp(1, -1); return cntPark <= k; }; ll l = 0, r = 1e15; while(l + 1 < r){ ll m = l+r>>1; if(check(m)){ r = m; } else l = m; } check(r); cout << r << "\n"; int cntUsed = 0; for(int i = 1; i <= n; i++){ if(choosed[i]){ cout << i << " "; cntUsed++; } } for(int i = 1; i <= n; i++){ if(cntUsed >= k) break; if(!choosed[i]){ cout << i <<" "; cntUsed++; } } cout << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "InvMOD" if(fopen(name".INP", "r")){ freopen(name".INP","r",stdin); freopen(name".OUT","w",stdout); } int t = 1; //cin >> t; while(t--) solve(); return 0; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(name".OUT","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...