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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <long double, pii> muchie;
typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST;
const ll NMAX = 10001;
const ll INF = (1LL << 60);
const ll HALF = (1LL << 59);
const ll MOD = 30013;
const ll BLOCK = 318;
const ll base = 31;
const ll nr_of_bits = 21;
int dp[NMAX][NMAX][2];
int n, k, root;
vector <pii> v[NMAX];
int sz[NMAX];
void DFS(int node, int p) {
int i;
dp[node][1][0] = dp[node][1][1] = 0;
sz[node] = 1;
for(auto x : v[node]) {
if(x.first == p)
continue;
DFS(x.first, node);
sz[node] += sz[x.first];
}
for(i = 2; i <= min(k, sz[node]); i++) {
dp[node][i][0] = dp[node][i][1] = 1e9;
}
int sum = 1;
for(auto x : v[node]) {
if(x.first == p)
continue;
for(int total = min(k, sum); total >= 1; total--) {
//debugs(total);
for(int subtree = 1; subtree <= min(k - total, sz[x.first]); subtree++) {
dp[node][total + subtree][0] = min(dp[node][total][0] + dp[x.first][subtree][0] + x.second * 2, dp[node][total + subtree][0]);
dp[node][total + subtree][1] = min(dp[node][total][1] + dp[x.first][subtree][0] + x.second * 2, dp[node][total + subtree][1]);
dp[node][total + subtree][1] = min(dp[node][total][0] + dp[x.first][subtree][1] + x.second, dp[node][total + subtree][1]);
}
}
sum += sz[x.first];
}
}
int main() {
int i;
cin >> n >> k >> root;
for(i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
v[a].push_back({b, c});
v[b].push_back({a, c});
}
DFS(root, 0);
cout << dp[root][k][1];
return 0;
}
# | 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... |