답안 #1081820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1081820 2024-08-30T11:35:13 Z vuavisao Chase (CEOI17_chase) C++14
100 / 100
321 ms 407932 KB
#include <bits/stdc++.h>
using namespace std;

const int N = (int) 1e5 + 1;

int n, breads;
int pigeons[N];
vector<int> g[N];

namespace sub1 {
	bool check() {
		return (n <= 10);
	}

	int parent[N];
	long long curPigeons[N];

	long long getDiff(int u, long long cur) {
		long long after = 0;
		while (u != 0) {
			after += curPigeons[u];
			u = parent[u];
		}
		return abs(after - cur);
	}

	long long dfs(int u, long long cur, int use) {
		long long res = 0;

		cur += curPigeons[u];
		res = max(res, getDiff(u, cur));
		for (const auto& v : g[u]) {
			if (v == parent[u]) continue;
			parent[v] = u;
			res = max(res, dfs(v, cur, use));
		}

		if (use < breads) {
			vector<long long> oldPigeons(n + 1, 0);
			oldPigeons[u] = curPigeons[u];
			for (const auto& v : g[u]) {
				oldPigeons[v] = curPigeons[v];
			}
			for (const auto& v : g[u]) {
				curPigeons[u] += curPigeons[v];
				curPigeons[v] = 0;
			}

			res = max(res, getDiff(u, cur));
			
			for (const auto& v : g[u]) {
				if (v == parent[u]) continue;
				parent[v] = u;
				res = max(res, dfs(v, cur, use + 1));
			}

			curPigeons[u] = oldPigeons[u];
			for (const auto& v : g[u]) {
				curPigeons[v] = oldPigeons[v];
			}
		}
		return res;
	}

	void solve() {
		long long res = 0;
		for (int root = 1; root <= n; ++ root) {
			for (int u = 1; u <= n; ++ u) {
				curPigeons[u] = pigeons[u];
				parent[u] = 0;
			}
			res = max(res, dfs(root, 0, 0));
		}
		cout << res;
	}
}

namespace sub2 {
	bool check() {
		return (n <= (int) 1e3);
	}

	long long dp[N][110];

	void dfs(int u, int p) {
		long long bonus = -pigeons[p];
		for (const auto& v : g[u]) {
			bonus += pigeons[v];
		}
		for (int use = breads - 1; use >= 0; -- use) {
			dp[u][use + 1] = max(dp[u][use + 1], dp[u][use] + bonus);
		}
		for (const auto& v : g[u]) {
			if (v == p) continue;
			for (int use = 0; use <= breads; ++ use) {
				dp[v][use] = max(dp[v][use], dp[u][use]);
			}
			dfs(v, u);
		}
	}

	long long calc(int root) {
		for (int u = 0; u <= n + 1; ++ u) {
			for (int use = 0; use <= breads + 1; ++ use) {
				dp[u][use] = 0;
			}
		}
		dfs(root, 0);
		long long res = 0;
		for (int u = 1; u <= n; ++ u) {
			for (int use = 0; use <= breads; ++ use) {
				res = max(res, dp[u][use]);
			}
		}
		return res;
	}

	void solve() {
		long long res = 0;
		for (int root = 1; root <= n; ++ root) {
			res = max(res, calc(root));
		}
		cout << res;
	}
}

namespace sub4 {
	pair<long long, int> dpOut[N][101][2];
	long long dpIn[N][101];
	long long cost[N];
	long long res = 0;

	long long getCost(int u, int p) {
		return cost[u] - pigeons[p];
	}

	void update(pair<long long, int> cur[], pair<long long, int> val) {
		pair<long long, int> old = cur[0];
		if (val.first > cur[0].first) {
			cur[0] = val;
			cur[1] = old;
		}
		else {
			cur[1] = max(cur[1], val);
		}
	}

	void dfsInOut(int u, int p) {
		for (int use = 0; use <= breads; ++ use) {
			dpOut[u][use][0] = (use == 0 ? make_pair(0ll, u) : make_pair(getCost(u, 0), u));
		}
		for (int use = 0; use <= breads; ++ use) {
			dpIn[u][use] = (use == 0 ? 0ll : getCost(u, p));
		}

		for (const auto& v : g[u]) {
			if (v == p) continue;
			dfsInOut(v, u);

			for (int use = 0; use <= breads; ++ use) {
				pair<long long, int> cur = make_pair(dpOut[v][use][0].first, v);
				if (use > 0) {
					cur.first = max(cur.first, dpOut[v][use - 1][0].first + getCost(u, v));
				}
				update(dpOut[u][use], cur);
			}

			for (int use = 0; use <= breads; ++ use) {
				long long cur = dpIn[v][use];
				dpIn[u][use] = max(dpIn[u][use], cur);
				if (use < breads) {
					cur += getCost(u, p);
					dpIn[u][use + 1] = max(dpIn[u][use + 1], cur);
				}
			}
		}

		for (const auto& v : g[u]) {
			if (v == p) continue;
			
			for (int useOut = 0; useOut <= breads; ++ useOut) {
				int useIn = breads - useOut;
				for (int typeOut = 0; typeOut < 2; ++ typeOut) {
					if (dpOut[u][useOut][typeOut].second == v) continue;
					res = max(res, dpOut[u][useOut][typeOut].first + dpIn[v][useIn]);
				}
			}
		}
		for (int useOut = 0; useOut <= breads; ++ useOut) {
			res = max(res, dpOut[u][useOut][0].first);
		}
		for (int useIn = 0; useIn <= breads; ++ useIn) {
			res = max(res, dpIn[u][useIn]);
		}
	}

	void solve() {
		for (int u = 1; u <= n; ++ u) {
			for (const auto& v : g[u]) {
				cost[v] += pigeons[u];
			}
		}
		dfsInOut(1, 0);
		cout << res;
	}

}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cin >> n >> breads;
	for (int u = 1; u <= n; ++ u) cin >> pigeons[u];
	for (int i = 2; i <= n; ++ i) {
		int u, v; cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	if (sub1::check()) {
		sub1::solve();
		return 0;
	}
	if (sub2::check()) {
		sub2::solve();
		return 0;
	}
	// cout << sub2::calc(1);
	sub4::solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2808 KB Output is correct
6 Correct 1 ms 2620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2808 KB Output is correct
6 Correct 1 ms 2620 KB Output is correct
7 Correct 282 ms 3676 KB Output is correct
8 Correct 27 ms 3676 KB Output is correct
9 Correct 23 ms 3672 KB Output is correct
10 Correct 283 ms 3676 KB Output is correct
11 Correct 112 ms 3676 KB Output is correct
12 Correct 42 ms 3672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 297 ms 407884 KB Output is correct
2 Correct 290 ms 407660 KB Output is correct
3 Correct 252 ms 402632 KB Output is correct
4 Correct 180 ms 402256 KB Output is correct
5 Correct 314 ms 402328 KB Output is correct
6 Correct 296 ms 402516 KB Output is correct
7 Correct 316 ms 402504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2808 KB Output is correct
6 Correct 1 ms 2620 KB Output is correct
7 Correct 282 ms 3676 KB Output is correct
8 Correct 27 ms 3676 KB Output is correct
9 Correct 23 ms 3672 KB Output is correct
10 Correct 283 ms 3676 KB Output is correct
11 Correct 112 ms 3676 KB Output is correct
12 Correct 42 ms 3672 KB Output is correct
13 Correct 297 ms 407884 KB Output is correct
14 Correct 290 ms 407660 KB Output is correct
15 Correct 252 ms 402632 KB Output is correct
16 Correct 180 ms 402256 KB Output is correct
17 Correct 314 ms 402328 KB Output is correct
18 Correct 296 ms 402516 KB Output is correct
19 Correct 316 ms 402504 KB Output is correct
20 Correct 316 ms 402512 KB Output is correct
21 Correct 171 ms 402256 KB Output is correct
22 Correct 317 ms 402516 KB Output is correct
23 Correct 182 ms 402436 KB Output is correct
24 Correct 311 ms 402260 KB Output is correct
25 Correct 263 ms 402632 KB Output is correct
26 Correct 314 ms 407892 KB Output is correct
27 Correct 321 ms 407932 KB Output is correct