답안 #391896

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391896 2021-04-20T05:21:30 Z parsabahrami Chase (CEOI17_chase) C++17
100 / 100
600 ms 487784 KB
/* There's someone in my head but it's not me */ 
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 1e5 + 2, K = 101;
int P[N], n, k; ll ans, M[N];
vector<int> adj[N]; pii dp[N][K], pd[N][K], idp[N][K], ipd[N][K];

void merge(ll v, ll u, ll x) {
	if (dp[v][x].S < max(dp[u][x].F, dp[u][x - 1].F + M[u] - P[v])) dp[v][x].S = max(dp[u][x].F, dp[u][x - 1].F + M[u] - P[v]), idp[v][x].S = u;
	if (dp[v][x].F < dp[v][x].S) swap(dp[v][x].F, dp[v][x].S), swap(idp[v][x].F, idp[v][x].S);
	if (pd[v][x].S < max(pd[u][x].F, pd[u][x - 1].F + M[v] - P[u])) pd[v][x].S = max(pd[u][x].F, pd[u][x - 1].F + M[v] - P[u]), ipd[v][x].S = u;
	if (pd[v][x].F < pd[v][x].S) swap(pd[v][x].F, pd[v][x].S), swap(ipd[v][x].F, ipd[v][x].S);
}

void DFS(ll v, ll p = -1) {
	pd[v][1].F = M[v]; ipd[v][1].F = v;
	for (ll u : adj[v]) {
		if (u != p) {
			DFS(u, v);
			for (ll i = 1; i < K; i++) {
				merge(v, u, i);
			}
		}
	}
	for (ll i = 0; i <= k; i++) {
		if (ipd[v][i].F == idp[v][k - i].F) ans = max({ans, pd[v][i].F + dp[v][k - i].S, pd[v][i].S + dp[v][k - i].F});
		else ans = max(ans, pd[v][i].F + dp[v][k - i].F);
	}
}

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++) 
        scanf("%d", &P[i]);
	for (int i = 1; i < n; i++) {
		int u, v; scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for (int i = 1; i <= n; i++) {
		for (int u : adj[i]) M[i] += P[u];
	}
	DFS(1);
	printf("%lld\n", ans);
    return 0;
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:41:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   41 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
chase.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   43 |         scanf("%d", &P[i]);
      |         ~~~~~^~~~~~~~~~~~~
chase.cpp:45:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   45 |   int u, v; scanf("%d%d", &u, &v);
      |             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 7 ms 7500 KB Output is correct
8 Correct 7 ms 7500 KB Output is correct
9 Correct 6 ms 5876 KB Output is correct
10 Correct 8 ms 7372 KB Output is correct
11 Correct 7 ms 7372 KB Output is correct
12 Correct 7 ms 7412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 565 ms 487560 KB Output is correct
2 Correct 544 ms 487624 KB Output is correct
3 Correct 432 ms 326340 KB Output is correct
4 Correct 535 ms 483756 KB Output is correct
5 Correct 600 ms 484000 KB Output is correct
6 Correct 583 ms 483400 KB Output is correct
7 Correct 586 ms 484344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 7 ms 7500 KB Output is correct
8 Correct 7 ms 7500 KB Output is correct
9 Correct 6 ms 5876 KB Output is correct
10 Correct 8 ms 7372 KB Output is correct
11 Correct 7 ms 7372 KB Output is correct
12 Correct 7 ms 7412 KB Output is correct
13 Correct 565 ms 487560 KB Output is correct
14 Correct 544 ms 487624 KB Output is correct
15 Correct 432 ms 326340 KB Output is correct
16 Correct 535 ms 483756 KB Output is correct
17 Correct 600 ms 484000 KB Output is correct
18 Correct 583 ms 483400 KB Output is correct
19 Correct 586 ms 484344 KB Output is correct
20 Correct 591 ms 484884 KB Output is correct
21 Correct 536 ms 483628 KB Output is correct
22 Correct 589 ms 484692 KB Output is correct
23 Correct 534 ms 483700 KB Output is correct
24 Correct 592 ms 483140 KB Output is correct
25 Correct 428 ms 325928 KB Output is correct
26 Correct 553 ms 487616 KB Output is correct
27 Correct 556 ms 487784 KB Output is correct