Submission #146564

# Submission time Handle Problem Language Result Execution time Memory
146564 2019-08-24T12:19:19 Z abacaba Dostavljač (COCI18_dostavljac) C++14
14 / 140
257 ms 2416 KB
#include <iostream>
#include <string>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <stack>
#include <stdio.h>
using namespace std;
 
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>

const int inf = 2e9;
const int N = 5e2 + 15;
int n, m, a[N], dp[2][N][N];
vector <int> g[N];

void dfs(int v, int p = -1) {
	for(int to : g[v])
		if(to != p) {
			dfs(to, v);
			for(int i = m; i; --i)
				for(int j = 0; j < i; ++j) {
					for(int k = 0; k < 2; ++k)
						dp[0][v][i] = max(dp[0][v][i], dp[k][to][j] + dp[1][v][i - j - 1]);
					if(i - j - 2 >= 0) {
						dp[0][v][i] = max(dp[0][v][i], dp[1][v][j] + dp[0][to][i - j - 2]);
						dp[1][v][i] = max(dp[1][v][i], dp[1][v][j] + dp[1][to][i - j - 2]);
					}
				}
		}
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	for(int i = 1; i < n; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i = 0; i < 2; ++i)
		for(int j = 1; j <= n; ++j)
			for(int k = 1; k <= m; ++k)
				dp[i][j][k] = a[j];
	dfs(1);
	printf("%d\n", max(dp[0][1][m], dp[1][1][m]));
	return 0;
}

Compilation message

dostavljac.cpp: In function 'int main()':
dostavljac.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~
dostavljac.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
dostavljac.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 504 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 632 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 760 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 988 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 69 ms 1196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 1528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 133 ms 2004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 257 ms 2416 KB Output isn't correct
2 Halted 0 ms 0 KB -