답안 #667322

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
667322 2022-12-01T06:24:48 Z dutinmeow Unique Cities (JOI19_ho_t5) C++17
100 / 100
429 ms 39604 KB
#include <bits/stdc++.h>

namespace std {

template<class Fun>
class y_combinator_result {
	Fun fun_;
public:
	template<class T>
	explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

	template<class ...Args>
	decltype(auto) operator()(Args &&...args) {
		return fun_(std::ref(*this), std::forward<Args>(args)...);
	}
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
	return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

} // namespace std

int main() {
	using namespace std;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int N, M;
	cin >> N >> M;
	vector<vector<int>> T(N);
	for (int i = 0; i < N - 1; i++) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		T[u].push_back(v);
		T[v].push_back(u);
	}
	vector<int> A(N);
	for (int &a : A) {
		cin >> a;
		a--;
	} 

	vector<int> dep(N), hei(N);

	auto dfs1 = y_combinator([&](auto self, int u, int p) -> void {
		for (int v : T[u]) {
			if (v == p)
				continue;
			dep[v] = dep[u] + 1;
			self(v, u);
		}
	});

	dep[0] = 0;
	dfs1(0, -1);
	int ds = max_element(dep.begin(), dep.end()) - dep.begin();
	dfs1(ds, -1);
	int dt = max_element(dep.begin(), dep.end()) - dep.begin();
	swap(ds, dt);

	auto dfs2 = y_combinator([&](auto self, int u, int p) -> int {
		hei[u] = -1;
		for (int v : T[u]) {
			if (v == p)
				continue;
			dep[v] = dep[u] + 1;
			hei[u] = max(hei[u], self(v, u));
		}
		return ++hei[u];
	});

	int num = 0;
	vector<int> ans(N, 0), frq(M, 0);
	stack<int> stk;

	auto dfs3 = y_combinator([&](auto self, int u, int p) -> void {
		int h = 0, g = -1; 
		for (int v : T[u]) 
			if (v != p && hei[v] + 1 > h)
				h = hei[v] + 1, g = v;
		int s = 0; 
		for (int v : T[u])
			if (v != p && v != g)
				s = max(s, hei[v] + 1);	
		if (g != -1) {
			while (!stk.empty() && dep[stk.top()] >= dep[u] - s) {
				int v = stk.top(); stk.pop();
				if (--frq[A[v]] == 0)
					num--;
			}
			stk.push(u);
			if (frq[A[u]]++ == 0)
				num++;
			self(g, u);
		}

		while (!stk.empty() && dep[stk.top()] >= dep[u] - h) {
			int v = stk.top(); stk.pop();
			if (--frq[A[v]] == 0)
				num--;
		}

		ans[u] = max(ans[u], num);
		for (int v : T[u]) {
			if (v == p || v == g)
				continue;
			if (stk.empty() || stk.top() != u) {
				stk.push(u);
				if (frq[A[u]]++ == 0)
					num++;
			}
			self(v, u);	
		}
		if (!stk.empty() && stk.top() == u) {
			stk.pop();
			if (--frq[A[u]] == 0)
				num--;
		}
	});

	dep[ds] = 0;
	dfs2(ds, -1);
	dfs3(ds, -1);

	dep[dt] = 0;
	dfs2(dt, -1);
	dfs3(dt, -1);

	for (int x : ans)
		cout << x << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 452 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 584 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 456 KB Output is correct
10 Correct 2 ms 432 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 2 ms 592 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 596 KB Output is correct
21 Correct 2 ms 536 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 3 ms 436 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 2 ms 592 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 137 ms 10316 KB Output is correct
2 Correct 171 ms 21424 KB Output is correct
3 Correct 21 ms 4060 KB Output is correct
4 Correct 266 ms 17732 KB Output is correct
5 Correct 378 ms 37116 KB Output is correct
6 Correct 373 ms 27336 KB Output is correct
7 Correct 296 ms 17896 KB Output is correct
8 Correct 296 ms 19740 KB Output is correct
9 Correct 276 ms 19136 KB Output is correct
10 Correct 319 ms 19164 KB Output is correct
11 Correct 135 ms 18440 KB Output is correct
12 Correct 356 ms 29848 KB Output is correct
13 Correct 305 ms 27260 KB Output is correct
14 Correct 318 ms 26668 KB Output is correct
15 Correct 118 ms 18400 KB Output is correct
16 Correct 297 ms 31056 KB Output is correct
17 Correct 312 ms 27492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 253 ms 15364 KB Output is correct
2 Correct 417 ms 37844 KB Output is correct
3 Correct 33 ms 4820 KB Output is correct
4 Correct 325 ms 19396 KB Output is correct
5 Correct 429 ms 39604 KB Output is correct
6 Correct 386 ms 29276 KB Output is correct
7 Correct 309 ms 19520 KB Output is correct
8 Correct 316 ms 23512 KB Output is correct
9 Correct 355 ms 22148 KB Output is correct
10 Correct 297 ms 20936 KB Output is correct
11 Correct 220 ms 19760 KB Output is correct
12 Correct 395 ms 35632 KB Output is correct
13 Correct 309 ms 27996 KB Output is correct
14 Correct 331 ms 28148 KB Output is correct
15 Correct 124 ms 20264 KB Output is correct
16 Correct 322 ms 33300 KB Output is correct
17 Correct 373 ms 29480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 452 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 584 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 456 KB Output is correct
10 Correct 2 ms 432 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 2 ms 592 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 596 KB Output is correct
21 Correct 2 ms 536 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 3 ms 436 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 2 ms 592 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 137 ms 10316 KB Output is correct
34 Correct 171 ms 21424 KB Output is correct
35 Correct 21 ms 4060 KB Output is correct
36 Correct 266 ms 17732 KB Output is correct
37 Correct 378 ms 37116 KB Output is correct
38 Correct 373 ms 27336 KB Output is correct
39 Correct 296 ms 17896 KB Output is correct
40 Correct 296 ms 19740 KB Output is correct
41 Correct 276 ms 19136 KB Output is correct
42 Correct 319 ms 19164 KB Output is correct
43 Correct 135 ms 18440 KB Output is correct
44 Correct 356 ms 29848 KB Output is correct
45 Correct 305 ms 27260 KB Output is correct
46 Correct 318 ms 26668 KB Output is correct
47 Correct 118 ms 18400 KB Output is correct
48 Correct 297 ms 31056 KB Output is correct
49 Correct 312 ms 27492 KB Output is correct
50 Correct 253 ms 15364 KB Output is correct
51 Correct 417 ms 37844 KB Output is correct
52 Correct 33 ms 4820 KB Output is correct
53 Correct 325 ms 19396 KB Output is correct
54 Correct 429 ms 39604 KB Output is correct
55 Correct 386 ms 29276 KB Output is correct
56 Correct 309 ms 19520 KB Output is correct
57 Correct 316 ms 23512 KB Output is correct
58 Correct 355 ms 22148 KB Output is correct
59 Correct 297 ms 20936 KB Output is correct
60 Correct 220 ms 19760 KB Output is correct
61 Correct 395 ms 35632 KB Output is correct
62 Correct 309 ms 27996 KB Output is correct
63 Correct 331 ms 28148 KB Output is correct
64 Correct 124 ms 20264 KB Output is correct
65 Correct 322 ms 33300 KB Output is correct
66 Correct 373 ms 29480 KB Output is correct
67 Correct 21 ms 2772 KB Output is correct
68 Correct 121 ms 16912 KB Output is correct
69 Correct 181 ms 17628 KB Output is correct
70 Correct 329 ms 17824 KB Output is correct
71 Correct 382 ms 37136 KB Output is correct
72 Correct 381 ms 27336 KB Output is correct
73 Correct 265 ms 18016 KB Output is correct
74 Correct 294 ms 21456 KB Output is correct
75 Correct 308 ms 19520 KB Output is correct
76 Correct 325 ms 19256 KB Output is correct
77 Correct 207 ms 18124 KB Output is correct
78 Correct 379 ms 31804 KB Output is correct
79 Correct 265 ms 29652 KB Output is correct
80 Correct 326 ms 25508 KB Output is correct
81 Correct 130 ms 18248 KB Output is correct
82 Correct 291 ms 31016 KB Output is correct
83 Correct 301 ms 27336 KB Output is correct
84 Correct 275 ms 18040 KB Output is correct
85 Correct 377 ms 37740 KB Output is correct
86 Correct 377 ms 27752 KB Output is correct
87 Correct 292 ms 18036 KB Output is correct
88 Correct 333 ms 22172 KB Output is correct
89 Correct 298 ms 20312 KB Output is correct
90 Correct 325 ms 19716 KB Output is correct
91 Correct 202 ms 18544 KB Output is correct
92 Correct 382 ms 37064 KB Output is correct
93 Correct 306 ms 24904 KB Output is correct
94 Correct 310 ms 22992 KB Output is correct
95 Correct 143 ms 18740 KB Output is correct
96 Correct 315 ms 31600 KB Output is correct
97 Correct 342 ms 28024 KB Output is correct
98 Correct 261 ms 19428 KB Output is correct
99 Correct 376 ms 38200 KB Output is correct
100 Correct 368 ms 28956 KB Output is correct
101 Correct 306 ms 18736 KB Output is correct
102 Correct 334 ms 21836 KB Output is correct
103 Correct 322 ms 20700 KB Output is correct
104 Correct 302 ms 20180 KB Output is correct
105 Correct 235 ms 18880 KB Output is correct
106 Correct 404 ms 30096 KB Output is correct
107 Correct 321 ms 30372 KB Output is correct
108 Correct 406 ms 25372 KB Output is correct
109 Correct 144 ms 19772 KB Output is correct
110 Correct 356 ms 32424 KB Output is correct
111 Correct 386 ms 28936 KB Output is correct