# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
597792 | ThegeekKnight16 | Paths (RMI21_paths) | C++17 | 1074 ms | 5404 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;
const int MAXN = 2e3 + 10;
long long int dp[MAXN][MAXN];
// ficar de olho no cache
long long int dp2[MAXN][MAXN];
vector<int> grafo[MAXN];
vector<long long int> 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] = 0LL;
for (int i = 0; i < grafo[v].size(); i++)
{
int viz = grafo[v][i];
long long 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] = 0LL;
for (int i = 1; i <= grafo[v].size(); i++)
{
int viz = grafo[v][i-1];
long long 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] = 0LL;
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;
long long int 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)
# | 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... |