Submission #534877

#TimeUsernameProblemLanguageResultExecution timeMemory
534877kartelParkovi (COCI22_parkovi)C++17
110 / 110
1659 ms40392 KiB
#include <bits/stdc++.h> //#include<ext/rope> #include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC target("avx2") //#pragma comment(linker, "/STACK:268435456") #define in(x) freopen(x, "r", stdin) #define out(x) freopen(x, "w", stdout) #define F first #define S second #define pb push_back #define sz(x) int(x.size()) #define el '\n' #define all(x) x.begin(), x.end() using namespace std; using namespace __gnu_pbds; //using namespace __gnu_cxx; typedef long long ll; typedef long double ld; typedef short int si; typedef unsigned long long ull; typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int N = 2e5 + 500; vector <int> ans; ll dp_down[N], dp_up[N], D; int n, k; int u, v, w; vector <pair <int, int> > g[N]; void dfs(int v, int pr, ll d) { dp_down[v] = 1e18; dp_up[v] = 0; for (auto [u, w] : g[v]) { if (u == pr) { continue; } dfs(u, v, w); dp_down[v] = min(dp_down[v], dp_down[u]); dp_up[v] = max(dp_up[v], dp_up[u]); } if (dp_down[v] + dp_up[v] <= D) { dp_up[v] = -1e18; } if (dp_up[v] != -1e18 && (pr == -1 || d + dp_up[v] > D)) { ans.pb(v); dp_down[v] = 0; dp_up[v] = -1e18; } dp_down[v] += d; dp_up[v] += d; } int main() { // freopen("input.txt", "r", stdin); // freopen("anti.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for (int i = 0; i < n - 1; i++) { cin >> u >> v >> w; g[v].pb({u, w}); g[u].pb({v, w}); } ll l = 0; ll r = (ll)1e18; while (l < r) { ll md = (l + r) >> 1; ans.clear(); D = md; dfs(1, -1, 0); if (sz(ans) > k) { l = md + 1; } else { r = md; } } D = l; ans.clear(); dfs(1, -1, 0); map <int, int> used; for (auto x : ans) { used[x] = 1; } for (int i = 1; i <= n; i++) { if (!used[i] && sz(ans) < k) { ans.pb(i); } } cout << l << el; for (auto x : ans) { cout << x << " "; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...