This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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], pre[MAXN];
bool inter(ii a, ii b){
return !(a.fi > b.se or a.se < b.fi);
}
bool check(int mid){
for(int i = 1; i < n; i++){
pre[i] = pre[i - 1] + edge[i].fi;
// cout << pre[i] << ' ';
}
// cout << endl;
vector<ii> Ranges;
for(int i = 1; i <= n; i++){
int lo = 1, hi = i, leftboard = 0;
while(lo <= hi){
int m = (lo + hi) / 2;
if (pre[i - 1] - pre[m - 1] <= mid){
leftboard = m;
hi = m - 1;
}
else lo = m + 1;
}
lo = i, hi = n;
int rightboard = 0;
while(lo <= hi){
int m = (lo + hi) / 2;
if (pre[m - 1] - pre[i - 1] <= mid){
// cout << mid << ' ' << pre[mid - 1] - pre[i - 1] << ' ' << mid << endl;
rightboard = m;
lo = m + 1;
}
else hi = m - 1;
}
// cout << i << ' ' << leftboard << ' ' << rightboard << endl;
Ranges.pb({leftboard, rightboard});
}
sort(Ranges.begin(), Ranges.end());
int numgroup = 0;
for(int l = 0, last = 0; l < Ranges.size(); l = last + 1){
last = l;
ii intersect = Ranges[l];
while(last < Ranges.size() - 1 and inter(intersect, Ranges[last + 1])){
last++;
intersect = ii(max(intersect.fi, Ranges[last].fi),
min(intersect.se, Ranges[last].se));
}
numgroup++;
}
return numgroup <= k;
}
void SolveSubtask3(){
int l = 1;
int r = 1e18;
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:171:37: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
171 | for(int l = 0, last = 0; l < Ranges.size(); l = last + 1){
| ~~^~~~~~~~~~~~~~~
Main.cpp:174:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
174 | while(last < Ranges.size() - 1 and inter(intersect, Ranges[last + 1])){
| ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp: At global scope:
Main.cpp:230:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
230 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:234:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
234 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:235:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
235 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |