# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1056222 | manhlinh1501 | Parkovi (COCI22_parkovi) | C++17 | 36 ms | 15444 KiB |
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>
using namespace std;
using pii = pair<int, int>;
using i64 = long long;
using pli = pair<i64, int>;
const int MAXN = 2e5 + 5;
const i64 oo64 = 1e18;
#define ALL(a) (a).begin(), (a).end()
int N, K;
vector<pii> adj[MAXN];
namespace subtask {
const int MAXN = 25;
bool is_subtask() {
return N <= 20;
}
i64 dist[MAXN][MAXN];
void DFS(int r, int u, int p) {
for(auto [v, w] : adj[u]) {
if(v == p) continue;
dist[r][v] = dist[r][u] + w;
DFS(r, v, u);
}
}
void solution() {
if(is_subtask() == false) return;
for(int i = 1; i <= N; i++) {
dist[i][i] = 0;
DFS(i, i, 0);
}
i64 ans = oo64;
int best = 0;
for(int mask = 0; mask < (1 << N); mask++) {
if(__builtin_popcount(mask) != K) continue;
vector<int> b(N + 1, 0);
for(int i = 0; i < N; i++) b[i + 1] = (mask >> i & 1);
i64 res = 0;
for(int i = 1; i <= N; i++) {
i64 minn = oo64;
for(int j = 1; j <= N; j++) {
if(b[j])
minn = min(minn, dist[i][j]);
}
res = max(res, minn);
}
if(ans > res) {
ans = res;
best = mask;
}
}
cout << ans;
cout << "\n";
for(int i = 0; i < N; i++) {
if(best >> i & 1) cout << i + 1 << " ";
}
exit(0);
}
}
signed main() {
#define TASK "code"
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> K;
for(int i = 1; i < N; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
subtask::solution();
return (0 ^ 0);
}
Compilation message (stderr)
# | 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... |