답안 #146565

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146565 2019-08-24T12:22:23 Z abacaba Dostavljač (COCI18_dostavljac) C++14
140 / 140
253 ms 2424 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][to][j] + dp[0][v][i - j - 2]);
						dp[1][v][i] = max(dp[1][v][i], dp[1][to][j] + dp[1][v][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:60: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:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
dostavljac.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 760 KB Output is correct
2 Correct 5 ms 760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 888 KB Output is correct
2 Correct 38 ms 888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 1244 KB Output is correct
2 Correct 23 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 1528 KB Output is correct
2 Correct 152 ms 1612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 131 ms 2040 KB Output is correct
2 Correct 54 ms 2040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 253 ms 2424 KB Output is correct
2 Correct 27 ms 2300 KB Output is correct