Submission #964314

#TimeUsernameProblemLanguageResultExecution timeMemory
964314RequiemParkovi (COCI22_parkovi)C++17
20 / 110
165 ms41080 KiB
#include<bits/stdc++.h> #define int long long #define pb push_back #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); #define MOD 1000000007 #define INF 1e18 #define fi first #define se second #define endl "\n" #define FOR(i,a,b) for(int i=a;i<=b;i++) #define FORD(i,a,b) for(int i=a;i>=b;i--) #define sz(a) ((int)(a).size()) #define pi 3.14159265359 #define TASKNAME "parkovi" template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; } template<typename T> bool minimize(T &res, const T &val) { if (res >= val){ res = val; return true; }; return false; } using namespace std; typedef pair<int,int> ii; typedef pair<int,ii> iii; typedef vector<int> vi; const int MAXN = 3e5 + 9; vector<int> g[MAXN]; iii edge[MAXN]; int n, k; namespace sub1{ bool checkSubtask1(){ return n <= 20; } int dist[21]; vector<int> SetVertice; void bfs(){ priority_queue<ii, vector<ii>, greater<ii> > q; fill(dist + 1, dist + 1 + n, INF); for(auto x: SetVertice){ dist[x] = 0; q.push({0, x}); } while(!q.empty()){ int u = q.top().se; int du = q.top().fi; q.pop(); if (du > dist[u]) continue; for(auto idedge: g[u]){ int v = edge[idedge].se.fi + edge[idedge].se.se - u; int w = edge[idedge].fi; if (dist[v] > dist[u] + w){ dist[v] = dist[u] + w; q.push({dist[v], v}); } } } } void solveSubtask1(){ int res = INF; int msk = 0; for(int i = 1; i < (1 << n); i++){ SetVertice.clear(); if (__builtin_popcount(i) != k) continue; for(int j = 0; j < n; j++){ if (i & (1 << j)) SetVertice.pb(j + 1); } bfs(); if (minimize(res, *max_element(dist + 1, dist + 1 + n))) msk = i; } cout << res << endl; vector<int> park; for(int i = 0; i < n; i++){ if (msk & (1 << i)) park.pb(i + 1); } for(auto x: park){ cout << x << " "; } cout << endl; } } namespace sub2{ bool checkSubtask2(){ return k == 1; } int par[MAXN], depth[MAXN]; void dfs(int u,int p){ for(auto idedge: g[u]){ int v = edge[idedge].se.fi + edge[idedge].se.se - u; int w = edge[idedge].fi; if (v == p) continue; depth[v] = depth[u] + w; par[v] = u; dfs(v, u); } } void reset(){ fill(par + 1, par + 1 + n, -1); fill(depth + 1, depth + 1 + n, 0); } void solveSubtask2(){ dfs(1, -1); int root = max_element(depth + 1, depth + 1 + n) - depth; reset(); dfs(root, -1); int furthest = max_element(depth + 1, depth + 1 + n) - depth; vector<int> diameter; int res = INF, tmp = furthest, chosen = 0; while(tmp != root) { if (minimize(res, max(depth[tmp], depth[furthest] - depth[tmp]))){ chosen = tmp; } tmp = par[tmp]; } cout << res << endl; cout << chosen << endl; } } namespace sub3{ bool checkSubtask3(){ for(int i = 1; i < n; i++){ if (edge[i].se.fi != i or edge[i].se.se != i + 1) return false; } return true; } int chosen[MAXN]; bool check(int mid){ int minimum = 0; queue<int> q; q.push(1); int tmp = k; int cur = mid; while(!q.empty()){ int u = q.front(); // cout << u << ' ' << cur << endl; if (cur < 0){ cur = mid; tmp--; } q.pop(); for(auto idedge: g[u]){ int v = edge[idedge].se.fi + edge[idedge].se.se - u; int w = edge[idedge].fi; if (v == u - 1) continue; if (cur - w < 0){ tmp--; cur = mid - w; } else cur = cur - w; q.push(v); } } return tmp >= 0; } void SolveSubtask3(){ int l = 1; int r = 2e14; int res= 0; while(l <= r){ int mid = (l + r) >> 1; if (check(mid)) { res = mid; r = mid - 1; } else l = mid + 1; } queue<int> q; q.push(1); int cur = res; while(!q.empty()){ int u = q.front(); q.pop(); if (cur < 0){ cur = res; k--; chosen[u] = 1; } for(auto idedge: g[u]){ int v = edge[idedge].se.fi + edge[idedge].se.se - u; int w = edge[idedge].fi; if (v == u - 1) continue; if (cur - w < 0){ k--; cur = res - w; chosen[u] = 1; } else cur -= w; q.push(v); } } for(int i = 1; i <= n; i++){ if (!chosen[i] and k > 0) chosen[i] = 1, k--; } cout << res << endl; for(int i = 1; i <= n; i++){ if (chosen[i]) cout << i << " "; } cout << endl; } } main() { fast; if (fopen(TASKNAME".inp","r")){ freopen(TASKNAME".inp","r",stdin); freopen(TASKNAME".out","w",stdout); } cin >> n >> k; for(int i = 1; i < n; i++){ int u, v, w; cin >> u >> v >> w; edge[i] = {w, {u, v}}; g[u].pb(i); g[v].pb(i); } if (sub1::checkSubtask1()) return sub1::solveSubtask1(), 0; if (sub2::checkSubtask2()) return sub2::solveSubtask2(), 0; if (sub3::checkSubtask3()) return sub3::SolveSubtask3(), 0; }

Compilation message (stderr)

Main.cpp: In function 'bool sub3::check(long long int)':
Main.cpp:136:13: warning: unused variable 'minimum' [-Wunused-variable]
  136 |         int minimum = 0;
      |             ^~~~~~~
Main.cpp: At global scope:
Main.cpp:210:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  210 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:214:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  214 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:215:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  215 |         freopen(TASKNAME".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...