:heavy_check_mark: test/tree/rerooting_dp/aoj_1595.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/1595

#include "src/tree/rerooting_dp.hpp"

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
	int n;
	cin >> n;
	auto f = [](int a, int b) { return max(a, b); };
	auto g = [](int a, int b) { return a + b; };
	RerootingDP<int, int, decltype(f), decltype(g)> dp(n, f, g, 0);
	for (int i = 0; i < n - 1; ++i) {
		int u, v;
		cin >> u >> v;
		--u;
		--v;
		dp.add_edge(u, v, 1);
	}

	dp.build();
	for (auto e : dp.reroot()) cout << 2 * (n - 1) - e << endl;

	return 0;
}
#line 1 "test/tree/rerooting_dp/aoj_1595.test.cpp"
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/1595

#line 1 "src/tree/rerooting_dp.hpp"



#include <utility>
#include <vector>

template<class T, class E, class F, class G>
class RerootingDP {
private:
	std::vector<std::vector<std::pair<int, E>>> graph;
	std::vector<T> dp1, dp2;
	F f; // function<T(T, T)>
	G g; // function<T(T, E)>
	T ti;

	void dfs(int v, int p) {
		for (auto const &[nv, e] : graph[v]) {
			if (nv == p) continue;
			dfs(nv, v);
			dp1[v] = f(dp1[v], g(dp1[nv], e));
		}
	}

	void dfs2(int v, int p, T ndp) {
		int size = graph[v].size();
		std::vector<T> lcum(size + 1, ti), rcum(size + 1, ti);

		for (int i = 0; i < size; ++i) {
			auto const &[nv, e] = graph[v][i];
			if (nv == p) lcum[i + 1] = f(lcum[i], g(ndp, e));
			else lcum[i + 1] = f(lcum[i], g(dp1[nv], e));
		}
		for (int i = size - 1; i >= 0; --i) {
			auto const &[nv, e] = graph[v][i];
			if (nv == p) rcum[i] = f(g(ndp, e), rcum[i + 1]);
			else rcum[i] = f(g(dp1[nv], e), rcum[i + 1]);
		}

		dp2[v] = lcum.back();
		for (int i = 0; i < size; ++i) {
			auto const &[nv, e] = graph[v][i];
			if (nv == p) continue;
			dfs2(nv, v, f(lcum[i], rcum[i + 1]));
		}
	}

public:
	RerootingDP(int n, F f, G g, T ti) :
		graph(n), dp1(n), dp2(n), f(f), g(g), ti(std::move(ti)) {}

	void add_edge(int u, int v, E const &e) {
		graph[u].emplace_back(v, e);
		graph[v].emplace_back(u, e);
	}

	std::vector<T> build(int v = 0, int p = -1) {
		dfs(v, p);
		return dp1;
	}

	std::vector<T> reroot(int v = 0, int p = -1) { return reroot(v, p, ti); }

	std::vector<T> reroot(int v, int p, T ndp) {
		dfs2(v, p, ndp);
		return dp2;
	}
};


#line 4 "test/tree/rerooting_dp/aoj_1595.test.cpp"

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
	int n;
	cin >> n;
	auto f = [](int a, int b) { return max(a, b); };
	auto g = [](int a, int b) { return a + b; };
	RerootingDP<int, int, decltype(f), decltype(g)> dp(n, f, g, 0);
	for (int i = 0; i < n - 1; ++i) {
		int u, v;
		cin >> u >> v;
		--u;
		--v;
		dp.add_edge(u, v, 1);
	}

	dp.build();
	for (auto e : dp.reroot()) cout << 2 * (n - 1) - e << endl;

	return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ testcase_00 :heavy_check_mark: AC 14 ms 7 MB
g++ testcase_01 :heavy_check_mark: AC 13 ms 7 MB
g++ testcase_02 :heavy_check_mark: AC 270 ms 30 MB
g++ testcase_03 :heavy_check_mark: AC 271 ms 30 MB
g++ testcase_04 :heavy_check_mark: AC 287 ms 30 MB
g++ testcase_05 :heavy_check_mark: AC 289 ms 30 MB
g++ testcase_06 :heavy_check_mark: AC 285 ms 30 MB
g++ testcase_07 :heavy_check_mark: AC 279 ms 30 MB
g++ testcase_08 :heavy_check_mark: AC 281 ms 30 MB
g++ testcase_09 :heavy_check_mark: AC 293 ms 30 MB
g++ testcase_10 :heavy_check_mark: AC 275 ms 30 MB
g++ testcase_11 :heavy_check_mark: AC 297 ms 30 MB
g++ testcase_12 :heavy_check_mark: AC 277 ms 30 MB
g++ testcase_13 :heavy_check_mark: AC 279 ms 30 MB
g++ testcase_14 :heavy_check_mark: AC 277 ms 30 MB
g++ testcase_15 :heavy_check_mark: AC 268 ms 30 MB
g++ testcase_16 :heavy_check_mark: AC 294 ms 30 MB
g++ testcase_17 :heavy_check_mark: AC 268 ms 30 MB
g++ testcase_18 :heavy_check_mark: AC 288 ms 30 MB
g++ testcase_19 :heavy_check_mark: AC 288 ms 30 MB
g++ testcase_20 :heavy_check_mark: AC 273 ms 30 MB
g++ testcase_21 :heavy_check_mark: AC 274 ms 30 MB
g++ testcase_22 :heavy_check_mark: AC 375 ms 135 MB
g++ testcase_23 :heavy_check_mark: AC 233 ms 28 MB
clang++ testcase_00 :heavy_check_mark: AC 12 ms 15 MB
clang++ testcase_01 :heavy_check_mark: AC 12 ms 11 MB
clang++ testcase_02 :heavy_check_mark: AC 276 ms 34 MB
clang++ testcase_03 :heavy_check_mark: AC 286 ms 30 MB
clang++ testcase_04 :heavy_check_mark: AC 299 ms 30 MB
clang++ testcase_05 :heavy_check_mark: AC 285 ms 34 MB
clang++ testcase_06 :heavy_check_mark: AC 294 ms 34 MB
clang++ testcase_07 :heavy_check_mark: AC 280 ms 34 MB
clang++ testcase_08 :heavy_check_mark: AC 303 ms 34 MB
clang++ testcase_09 :heavy_check_mark: AC 278 ms 34 MB
clang++ testcase_10 :heavy_check_mark: AC 294 ms 34 MB
clang++ testcase_11 :heavy_check_mark: AC 303 ms 32 MB
clang++ testcase_12 :heavy_check_mark: AC 305 ms 32 MB
clang++ testcase_13 :heavy_check_mark: AC 286 ms 32 MB
clang++ testcase_14 :heavy_check_mark: AC 297 ms 32 MB
clang++ testcase_15 :heavy_check_mark: AC 294 ms 34 MB
clang++ testcase_16 :heavy_check_mark: AC 288 ms 34 MB
clang++ testcase_17 :heavy_check_mark: AC 281 ms 34 MB
clang++ testcase_18 :heavy_check_mark: AC 285 ms 32 MB
clang++ testcase_19 :heavy_check_mark: AC 289 ms 34 MB
clang++ testcase_20 :heavy_check_mark: AC 282 ms 32 MB
clang++ testcase_21 :heavy_check_mark: AC 285 ms 36 MB
clang++ testcase_22 :heavy_check_mark: AC 324 ms 77 MB
clang++ testcase_23 :heavy_check_mark: AC 231 ms 30 MB
Back to top page