Submission #1080364

# Submission time Handle Problem Language Result Execution time Memory
1080364 2024-08-29T09:13:15 Z wiwiho Team Coding (EGOI24_teamcoding) C++14
100 / 100
604 ms 127460 KB
#include <bits/stdc++.h>
using namespace std;

#define iter(v) v.begin(), v.end()
#define pb emplace_back
#define ff first
#define ss second
#define SZ(v) int(v.size())

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#ifdef zisk
void debug() { cerr << "\n"; }
template<class T, class ... U>
void debug(T a, U... b) {
	cerr << a << " ", debug(b...);
}
template<class T>
void pary(T l, T r){
	while (l != r) cerr << *l << " ", l++;
	cerr << "\n";
}
#else
#define debug(...) void()
#define pary(...) void()
#endif

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

	int n, K;
	cin >> n >> K;
	const int B = 400;

	vector<int> color(n);
	vector<vector<int>> pos(K);
	for (int i = 0; i < n; i++) {
		cin >> color[i];
		pos[color[i]].pb(i);
	}

	vector<vector<int>> g(n);
	for (int i = 1; i < n; i++) {
		int p;
		cin >> p;
		g[p].pb(i);
	}

	vector<int> dpt(n), in(n), out(n);
	int ts = 0;
	vector<int> cur(K, -1);
	vector<bool> top(n);
	auto dfs1 = [&](auto dfs, int now) -> void {
		in[now] = ts++;
		if (cur[color[now]] == -1) {
			cur[color[now]] = now;
			top[now] = true;
		}
		for (int i : g[now]) {
			dpt[i] = dpt[now] + 1;
			dfs(dfs, i);
		}
		if (cur[color[now]] == now)
			cur[color[now]] = -1;
		out[now] = ts++;
	};
	dfs1(dfs1, 0);

	auto isAnc = [&](int a, int b) {
		return in[a] <= in[b] && out[b] <= out[a];
	};

	int ans_member = 0, ans_swp = 0;

	auto solve_big = [&](int c) {
		//debug("solve_big", c);
		
		vector<int> total(n + 1);
		for (int i = 0; i < n; i++)
			if (color[i] == c) total[dpt[i]]++;

		vector<int> cnt(n + 1), sz(n + 1);
		auto dfs2 = [&](auto dfs, int now, int type) -> void {
			sz[dpt[now]] += type;
			if (color[now] == c) cnt[dpt[now]] += type;
			for (int i : g[now]) dfs(dfs, i, type);
		};

		for (int i = 0; i < n; i++)
			if (color[i] == c && top[i]) {
				dfs2(dfs2, i, 1);
				int member = 0, swp = 0;
				for (int j = dpt[i]; j <= n && sz[j]; j++) {
					//debug("depth", j, sz[j], cnt[j], total[j]);
					int tmp = min(total[j], sz[j]);
					member += tmp;
					swp += tmp - cnt[j];
				}
				//debug("top", i, member, swp);
				if (member > ans_member) ans_member = member, ans_swp = swp;
				else if (member == ans_member && swp < ans_swp) ans_swp = swp;
				dfs2(dfs2, i, -1);
			}
	};

	for (int i = 0; i < K; i++) 
		if (SZ(pos[i]) > B)
			solve_big(i);

	{
		vector<deque<int>> down(n);
		vector<int> total(n), cnt(n);
		vector<bool> vst(n);
		auto dfs3 = [&](auto dfs, int now) -> void {
			down[now].pb(1);
			for (int i : g[now]) {
				dfs(dfs, i);
				down[i].emplace_front(0);
				if (SZ(down[i]) > SZ(down[now]))
					down[now].swap(down[i]);
				for (int j = 0; j < SZ(down[i]); j++)
					down[now][j] += down[i][j];
			}
			//debug("dfs3", now);
			//pary(iter(down[now]));
			if (SZ(pos[color[now]]) > B) return;
			for (int i : pos[color[now]]) {
				total[dpt[i]]++;
				if (isAnc(now, i)) cnt[dpt[i]]++;
			}
			int member = 0, swp = 0;
			for (int i : pos[color[now]]) {
				int d = dpt[i];
				if (vst[d]) continue;
				vst[d] = true;
				if (d < dpt[now] || d >= dpt[now] + SZ(down[now])) continue;
				int tmp = min(total[d], down[now][d - dpt[now]]);
				member += tmp;
				swp += tmp - cnt[d];
			}
			if (member > ans_member) ans_member = member, ans_swp = swp;
			else if (member == ans_member && swp < ans_swp) ans_swp = swp;
			for (int i : pos[color[now]]) {
				int d = dpt[i];
				total[d] = 0;
				cnt[d] = 0;
				vst[d] = false;
			}
		};
		dfs3(dfs3, 0);
	}

	cout << ans_member << " " << ans_swp << "\n";

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 85 ms 100944 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 71 ms 98988 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 2 ms 2512 KB Output is correct
15 Correct 97 ms 103788 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 77 ms 98432 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 2 ms 2392 KB Output is correct
8 Correct 103 ms 100404 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 125 ms 111360 KB Output is correct
12 Correct 85 ms 124860 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 126 ms 100304 KB Output is correct
16 Correct 131 ms 100272 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 125 ms 100228 KB Output is correct
20 Correct 132 ms 100128 KB Output is correct
21 Correct 126 ms 100132 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 97 ms 100920 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 94 ms 103660 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 2 ms 2392 KB Output is correct
16 Correct 2 ms 2140 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 153 ms 104948 KB Output is correct
19 Correct 147 ms 96080 KB Output is correct
20 Correct 141 ms 89684 KB Output is correct
21 Correct 143 ms 113236 KB Output is correct
22 Correct 143 ms 121712 KB Output is correct
23 Correct 134 ms 90964 KB Output is correct
24 Correct 120 ms 86356 KB Output is correct
25 Correct 2 ms 2140 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 2 ms 2400 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
29 Correct 11 ms 10848 KB Output is correct
30 Correct 156 ms 103912 KB Output is correct
31 Correct 154 ms 97612 KB Output is correct
32 Correct 144 ms 92896 KB Output is correct
33 Correct 150 ms 116308 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 2652 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 356 KB Output is correct
20 Correct 3 ms 2396 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 456 KB Output is correct
23 Correct 2 ms 2396 KB Output is correct
24 Correct 2 ms 2140 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 2 ms 2136 KB Output is correct
27 Correct 2 ms 2392 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
29 Correct 2 ms 2396 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 3 ms 1884 KB Output is correct
32 Correct 2 ms 2400 KB Output is correct
33 Correct 2 ms 2408 KB Output is correct
34 Correct 5 ms 2652 KB Output is correct
35 Correct 4 ms 2916 KB Output is correct
36 Correct 3 ms 2656 KB Output is correct
37 Correct 2 ms 2404 KB Output is correct
38 Correct 0 ms 352 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 356 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 85 ms 100944 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 71 ms 98988 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 2 ms 2512 KB Output is correct
15 Correct 97 ms 103788 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 2 ms 2396 KB Output is correct
21 Correct 77 ms 98432 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 2 ms 2392 KB Output is correct
24 Correct 103 ms 100404 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 125 ms 111360 KB Output is correct
28 Correct 85 ms 124860 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 126 ms 100304 KB Output is correct
32 Correct 131 ms 100272 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 125 ms 100228 KB Output is correct
36 Correct 132 ms 100128 KB Output is correct
37 Correct 126 ms 100132 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 97 ms 100920 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 2 ms 2396 KB Output is correct
49 Correct 94 ms 103660 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 2 ms 2392 KB Output is correct
54 Correct 2 ms 2140 KB Output is correct
55 Correct 2 ms 2648 KB Output is correct
56 Correct 153 ms 104948 KB Output is correct
57 Correct 147 ms 96080 KB Output is correct
58 Correct 141 ms 89684 KB Output is correct
59 Correct 143 ms 113236 KB Output is correct
60 Correct 143 ms 121712 KB Output is correct
61 Correct 134 ms 90964 KB Output is correct
62 Correct 120 ms 86356 KB Output is correct
63 Correct 2 ms 2140 KB Output is correct
64 Correct 2 ms 2396 KB Output is correct
65 Correct 2 ms 2400 KB Output is correct
66 Correct 2 ms 2396 KB Output is correct
67 Correct 11 ms 10848 KB Output is correct
68 Correct 156 ms 103912 KB Output is correct
69 Correct 154 ms 97612 KB Output is correct
70 Correct 144 ms 92896 KB Output is correct
71 Correct 150 ms 116308 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 0 ms 348 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 2 ms 2396 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 2 ms 2396 KB Output is correct
86 Correct 0 ms 348 KB Output is correct
87 Correct 2 ms 2396 KB Output is correct
88 Correct 0 ms 348 KB Output is correct
89 Correct 2 ms 2396 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 3 ms 2652 KB Output is correct
92 Correct 0 ms 348 KB Output is correct
93 Correct 0 ms 348 KB Output is correct
94 Correct 0 ms 356 KB Output is correct
95 Correct 3 ms 2396 KB Output is correct
96 Correct 1 ms 348 KB Output is correct
97 Correct 0 ms 456 KB Output is correct
98 Correct 2 ms 2396 KB Output is correct
99 Correct 2 ms 2140 KB Output is correct
100 Correct 2 ms 2652 KB Output is correct
101 Correct 2 ms 2136 KB Output is correct
102 Correct 2 ms 2392 KB Output is correct
103 Correct 2 ms 2396 KB Output is correct
104 Correct 2 ms 2396 KB Output is correct
105 Correct 2 ms 2652 KB Output is correct
106 Correct 3 ms 1884 KB Output is correct
107 Correct 2 ms 2400 KB Output is correct
108 Correct 2 ms 2408 KB Output is correct
109 Correct 5 ms 2652 KB Output is correct
110 Correct 4 ms 2916 KB Output is correct
111 Correct 3 ms 2656 KB Output is correct
112 Correct 2 ms 2404 KB Output is correct
113 Correct 0 ms 352 KB Output is correct
114 Correct 0 ms 348 KB Output is correct
115 Correct 1 ms 356 KB Output is correct
116 Correct 0 ms 348 KB Output is correct
117 Correct 0 ms 348 KB Output is correct
118 Correct 0 ms 348 KB Output is correct
119 Correct 0 ms 348 KB Output is correct
120 Correct 0 ms 356 KB Output is correct
121 Correct 0 ms 344 KB Output is correct
122 Correct 0 ms 348 KB Output is correct
123 Correct 1 ms 460 KB Output is correct
124 Correct 3 ms 2448 KB Output is correct
125 Correct 94 ms 102240 KB Output is correct
126 Correct 1 ms 352 KB Output is correct
127 Correct 2 ms 2400 KB Output is correct
128 Correct 75 ms 99008 KB Output is correct
129 Correct 0 ms 352 KB Output is correct
130 Correct 2 ms 2520 KB Output is correct
131 Correct 82 ms 103592 KB Output is correct
132 Correct 0 ms 356 KB Output is correct
133 Correct 2 ms 2400 KB Output is correct
134 Correct 109 ms 100840 KB Output is correct
135 Correct 1 ms 344 KB Output is correct
136 Correct 2 ms 2652 KB Output is correct
137 Correct 140 ms 112132 KB Output is correct
138 Correct 90 ms 125116 KB Output is correct
139 Correct 1 ms 344 KB Output is correct
140 Correct 0 ms 348 KB Output is correct
141 Correct 143 ms 101032 KB Output is correct
142 Correct 120 ms 101048 KB Output is correct
143 Correct 1 ms 348 KB Output is correct
144 Correct 3 ms 2396 KB Output is correct
145 Correct 130 ms 100900 KB Output is correct
146 Correct 136 ms 101156 KB Output is correct
147 Correct 130 ms 101096 KB Output is correct
148 Correct 1 ms 348 KB Output is correct
149 Correct 0 ms 348 KB Output is correct
150 Correct 2 ms 2396 KB Output is correct
151 Correct 2 ms 2140 KB Output is correct
152 Correct 3 ms 2648 KB Output is correct
153 Correct 151 ms 104932 KB Output is correct
154 Correct 148 ms 96180 KB Output is correct
155 Correct 146 ms 89680 KB Output is correct
156 Correct 151 ms 113236 KB Output is correct
157 Correct 137 ms 121636 KB Output is correct
158 Correct 128 ms 90960 KB Output is correct
159 Correct 122 ms 86400 KB Output is correct
160 Correct 2 ms 2140 KB Output is correct
161 Correct 3 ms 2396 KB Output is correct
162 Correct 2 ms 2468 KB Output is correct
163 Correct 2 ms 2404 KB Output is correct
164 Correct 9 ms 10800 KB Output is correct
165 Correct 160 ms 103908 KB Output is correct
166 Correct 151 ms 97760 KB Output is correct
167 Correct 133 ms 92816 KB Output is correct
168 Correct 145 ms 116132 KB Output is correct
169 Correct 2 ms 2652 KB Output is correct
170 Correct 3 ms 1884 KB Output is correct
171 Correct 2 ms 2396 KB Output is correct
172 Correct 2 ms 2396 KB Output is correct
173 Correct 6 ms 2700 KB Output is correct
174 Correct 6 ms 2904 KB Output is correct
175 Correct 3 ms 2652 KB Output is correct
176 Correct 2 ms 2396 KB Output is correct
177 Correct 148 ms 116632 KB Output is correct
178 Correct 604 ms 77868 KB Output is correct
179 Correct 127 ms 119508 KB Output is correct
180 Correct 9 ms 10584 KB Output is correct
181 Correct 133 ms 102924 KB Output is correct
182 Correct 9 ms 11868 KB Output is correct
183 Correct 8 ms 12892 KB Output is correct
184 Correct 10 ms 12124 KB Output is correct
185 Correct 10 ms 12888 KB Output is correct
186 Correct 134 ms 118468 KB Output is correct
187 Correct 117 ms 126420 KB Output is correct
188 Correct 133 ms 117392 KB Output is correct
189 Correct 116 ms 126096 KB Output is correct
190 Correct 115 ms 125696 KB Output is correct
191 Correct 114 ms 127460 KB Output is correct
192 Correct 8 ms 10588 KB Output is correct
193 Correct 129 ms 101780 KB Output is correct
194 Correct 98 ms 101464 KB Output is correct
195 Correct 0 ms 348 KB Output is correct
196 Correct 0 ms 348 KB Output is correct
197 Correct 1 ms 348 KB Output is correct
198 Correct 0 ms 348 KB Output is correct