답안 #98163

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
98163 2019-02-21T04:06:15 Z luciocf Chase (CEOI17_chase) C++14
100 / 100
1027 ms 327600 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxv = 110;

typedef long long ll;

int n, c;
int num[maxn];

ll soma[maxn], ans;
ll d[2][maxn][maxv], ant[2][maxn][maxv], ant2[maxv];

vector<int> grafo[maxn];

void dfs(int u, int p)
{
	for (auto v: grafo[u])
	{
		if (v == p) continue;

		dfs(v, u);

		for (int i = 1; i <= c; i++)
		{
			ll D = max(d[0][v][i], d[0][v][i-1]+soma[v]-(ll)num[u]);

			if (D > d[0][u][i]) d[1][u][i] = d[0][u][i], d[0][u][i] = D;
			else if (D > d[1][u][i]) d[1][u][i] = D;
		}
	}
}

void rotate(int u, int p)
{
	for (int i = 1; i <= c; i++)
		ans = max({ans, d[0][u][i], d[0][u][i-1]+soma[u]});

	for (auto v: grafo[u])
	{
		if (v == p) continue;

		for (int i = 1; i <= c; i++)
			ant2[i] = d[0][v][i]; 

		for (int i = 1; i <= c; i++)
		{
			ant[0][u][i] = d[0][u][i], ant[1][u][i] = d[1][u][i];

			ll D = max(d[0][v][i], ant2[i-1]+soma[v]-(ll)num[u]);

			if (d[0][u][i] == D) d[0][u][i] = d[1][u][i];

			D = max(d[0][u][i], d[0][u][i-1]+soma[u]-(ll)num[v]);

			if (D > d[0][v][i]) d[1][v][i] = d[0][v][i], d[0][v][i] = D;
			else if (D > d[1][v][i]) d[1][v][i] = D;
		}

		rotate(v, u);

		for (int i = 1; i <= c; i++)
			d[0][u][i] = ant[0][u][i], d[1][u][i] = ant[1][u][i];
	}
}

int main(void)
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin >> n >> c;

	for (int i = 1; i <= n; i++)
		cin >> num[i];

	for (int i = 1; i < n; i++)
	{
		int u, v;
		cin >> u >> v;

		grafo[u].push_back(v); grafo[v].push_back(u);
	}

	for (int u = 1; u <= n; u++)
		for (auto v: grafo[u])
			soma[u] += (ll)num[v];

	dfs(1, 0); rotate(1, 0);

	cout << ans << "\n"; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2816 KB Output is correct
2 Correct 5 ms 2688 KB Output is correct
3 Correct 3 ms 2816 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2816 KB Output is correct
2 Correct 5 ms 2688 KB Output is correct
3 Correct 3 ms 2816 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 8 ms 5504 KB Output is correct
8 Correct 8 ms 5376 KB Output is correct
9 Correct 7 ms 4608 KB Output is correct
10 Correct 12 ms 5888 KB Output is correct
11 Correct 11 ms 6016 KB Output is correct
12 Correct 9 ms 5888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 775 ms 272468 KB Output is correct
2 Correct 751 ms 272376 KB Output is correct
3 Correct 392 ms 179784 KB Output is correct
4 Correct 457 ms 320696 KB Output is correct
5 Correct 996 ms 325484 KB Output is correct
6 Correct 1027 ms 325368 KB Output is correct
7 Correct 1016 ms 325112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2816 KB Output is correct
2 Correct 5 ms 2688 KB Output is correct
3 Correct 3 ms 2816 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 8 ms 5504 KB Output is correct
8 Correct 8 ms 5376 KB Output is correct
9 Correct 7 ms 4608 KB Output is correct
10 Correct 12 ms 5888 KB Output is correct
11 Correct 11 ms 6016 KB Output is correct
12 Correct 9 ms 5888 KB Output is correct
13 Correct 775 ms 272468 KB Output is correct
14 Correct 751 ms 272376 KB Output is correct
15 Correct 392 ms 179784 KB Output is correct
16 Correct 457 ms 320696 KB Output is correct
17 Correct 996 ms 325484 KB Output is correct
18 Correct 1027 ms 325368 KB Output is correct
19 Correct 1016 ms 325112 KB Output is correct
20 Correct 911 ms 325892 KB Output is correct
21 Correct 105 ms 9336 KB Output is correct
22 Correct 924 ms 327344 KB Output is correct
23 Correct 477 ms 322808 KB Output is correct
24 Correct 940 ms 327600 KB Output is correct
25 Correct 346 ms 181548 KB Output is correct
26 Correct 764 ms 274492 KB Output is correct
27 Correct 664 ms 274652 KB Output is correct