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;
const int MAXN = 2e3 + 10;
int dp[MAXN][MAXN];
// ficar de olho no cache
int dp2[MAXN][MAXN];
vector<int> grafo[MAXN], pesos[MAXN];
int pai[MAXN], pesoPai[MAXN], Marc[MAXN];
void dfs(int v, int K)
{
Marc[v] = 1;
for (int k = 0; k <= K; k++) dp[v][k] = 0;
for (int i = 0; i < grafo[v].size(); i++)
{
int viz = grafo[v][i];
int p = pesos[v][i];
if (Marc[viz] == 1) continue;
pai[viz] = v; pesoPai[viz] = p;
dfs(viz, K);
}
for (int k = 0; k <= K; k++) dp2[grafo[v].size()][k] = 0;
for (int i = 1; i <= grafo[v].size(); i++)
{
int viz = grafo[v][i-1];
int p = pesos[v][i-1];
if (pai[v] == viz)
{
for (int k = 0; k <= K; k++) dp2[i][k] = dp2[i-1][k];
continue;
}
//cerr << "v: " << v << ", testando viz: " << viz << '\n';
dp2[i][0] = 0;
for (int k = 1; k <= K; k++)
{
dp2[i][k] = dp2[i-1][k];
for (int q = 1; q <= k; q++) dp2[i][k] = max(dp2[i][k], dp2[i-1][k - q] + dp[viz][q] + p);
}
}
//cerr << "v: " << v << '\n';
for (int k = 1; k <= K; k++) {dp[v][k] = dp2[grafo[v].size()][k]; /*cerr << dp[v][k] << " ";*/}
//cerr << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
for (int i = 1; i < N; i++)
{
int X, Y, P;
cin >> X >> Y >> P;
grafo[X].push_back(Y);
pesos[X].push_back(P);
grafo[Y].push_back(X);
pesos[Y].push_back(P);
}
for (int i = 1; i <= N; i++)
{
for (int q = 1; q <= N; q++) Marc[q] = 0;
pai[i] = i;
dfs(i, K);
//cerr << '\n' << '\n';
cout << dp[i][K] << '\n';
}
}
Compilation message (stderr)
Main.cpp: In function 'void dfs(int, int)':
Main.cpp:15:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for (int i = 0; i < grafo[v].size(); i++)
| ~~^~~~~~~~~~~~~~~~~
Main.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i = 1; i <= grafo[v].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |