답안 #309579

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
309579 2020-10-03T20:19:05 Z Kenzo_1114 Chase (CEOI17_chase) C++17
70 / 100
497 ms 98808 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int MAXM = 110;

int n, m;
long long int p[MAXN], dp[MAXN][MAXM];
vector<int> graph[MAXN];

void dfs(int v, int par)
{
	long long int sumP = 0;

	for(int i = 0; i < graph[v].size(); i++)
	{
		int u = graph[v][i];
		if(u == par)	continue;

		sumP += p[u];
		dfs(u, v);
	}

	for(int k = 0; k <= m; k++)
	{
		dp[v][k] = 0;
		for(int i = 0; i < graph[v].size(); i++)
		{
			int u = graph[v][i];
			if(u == par)	continue;

			dp[v][k] = max(dp[v][k], dp[u][k]);
			if(k)	dp[v][k] = max(dp[v][k], dp[u][k - 1] + sumP);
		}
	}
}

int main ()
{
	scanf("%d %d", &n, &m);

	for(int i = 1; i <= n; i++)	scanf("%lld", &p[i]);

	for(int i = 0, u, v; i < n - 1; i++)
	{
		scanf("%d %d", &u, &v);
		graph[u].push_back(v);
		graph[v].push_back(u);
	}	

	long long int ans = 0;
	if(n <= 1000)
	{
		for(int i = 1; i <= n; i++)
		{
			dfs(i, i);
			for(int j = 1; j <= n; j++)	ans = max(ans, dp[j][m]);
		}
	}
	else
	{
		dfs(1, 1);
		for(int j = 1; j <= n; j++)		ans = max(ans, dp[j][m]);
	}

	printf("%lld\n", ans);
}

/*

12 2
2 3 3 8 1 5 6 7 8 3 5 4 
2 1
2 7
3 4 
4 7 
7 6 
5 6 
6 8 
6 9
7 10 
10 11 
10 12

*/

Compilation message

chase.cpp: In function 'void dfs(int, int)':
chase.cpp:14:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |  for(int i = 0; i < graph[v].size(); i++)
      |                 ~~^~~~~~~~~~~~~~~~~
chase.cpp:26:20: 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 = 0; i < graph[v].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~
chase.cpp: In function 'int main()':
chase.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
chase.cpp:41:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   41 |  for(int i = 1; i <= n; i++) scanf("%lld", &p[i]);
      |                              ~~~~~^~~~~~~~~~~~~~~
chase.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   45 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 486 ms 3704 KB Output is correct
8 Correct 51 ms 3584 KB Output is correct
9 Correct 52 ms 3584 KB Output is correct
10 Correct 497 ms 3704 KB Output is correct
11 Correct 182 ms 3584 KB Output is correct
12 Correct 77 ms 3584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 176 ms 98808 KB Output is correct
2 Correct 171 ms 98680 KB Output is correct
3 Correct 238 ms 95216 KB Output is correct
4 Correct 125 ms 94840 KB Output is correct
5 Correct 186 ms 94840 KB Output is correct
6 Correct 186 ms 94840 KB Output is correct
7 Correct 187 ms 94840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 486 ms 3704 KB Output is correct
8 Correct 51 ms 3584 KB Output is correct
9 Correct 52 ms 3584 KB Output is correct
10 Correct 497 ms 3704 KB Output is correct
11 Correct 182 ms 3584 KB Output is correct
12 Correct 77 ms 3584 KB Output is correct
13 Correct 176 ms 98808 KB Output is correct
14 Correct 171 ms 98680 KB Output is correct
15 Correct 238 ms 95216 KB Output is correct
16 Correct 125 ms 94840 KB Output is correct
17 Correct 186 ms 94840 KB Output is correct
18 Correct 186 ms 94840 KB Output is correct
19 Correct 187 ms 94840 KB Output is correct
20 Incorrect 187 ms 94944 KB Output isn't correct
21 Halted 0 ms 0 KB -