#include <bits/stdc++.h>
using namespace std;
void dfs(int x, vector<vector<int>> &AR, vector<int> &depth, int cur)
{
depth[x] = cur;
for (int i = 0; i < AR[x].size(); i++)
{
dfs(AR[x][i], AR, depth, cur + 1);
}
}
void dfs2(int x, vector<vector<int>> &AR, vector<vector<pair<int, int>>> &colorpos, vector<int> &available, map<int, int> &M, vector<int> &C, vector<vector<int>> &colors, int cur, int &best, int &best2)
{
available.push_back(1);
vector<int> available2;
map<int, int> M2;
M[C[x]] += 1;
for (int i = 0; i < AR[x].size(); i++)
{
available2.clear();
M2.clear();
dfs2(AR[x][i], AR, colorpos, available2, M2, C, colors, cur + 1, best, best2);
available2.push_back(0);
if (available2.size() > available.size())
swap(available2, available);
for (int j = 0; j < available2.size(); j++)
{
available[j + available.size() - available2.size()] += available2[j];
}
if (M2.size() > M.size())
swap(M, M2);
for (auto j : M2)
{
M[j.first] += j.second;
}
}
if (colors[C[x]].size() < sqrt(AR.size()))
{
int ans1 = 0, ans2 = 0;
for (int i = 0; i < colorpos[C[x]].size(); i++)
{
if (colorpos[C[x]][i].first >= cur)
ans1 += min(colorpos[C[x]][i].second, available[available.size() - colorpos[C[x]][i].first - 1 + cur]);
}
ans2 = ans1 - M[C[x]];
if (ans1 > best || (ans1 == best && ans2 < best2))
{
best = ans1, best2 = ans2;
}
}
}
int dfs3(int x, vector<vector<int>> &AR, vector<int> &C, int c, vector<int> &used, vector<int> &maxx, int cur, int &best, int &best2, int &bla)
{
vector<int> used2;
int tot = 0, tot2 = 0;
if (C[x] == c)
tot2++;
used.push_back(0);
if (maxx[cur] != 0)
{
used[0] = 1;
tot++;
}
for (int i = 0; i < AR[x].size(); i++)
{
used2.clear();
tot2 += dfs3(AR[x][i], AR, C, c, used2, maxx, cur + 1, best, best2, bla);
used2.push_back(0);
if (used2.size() > used.size())
{
swap(used2, used);
swap(tot, bla);
}
for (int j = 0; j < used2.size(); j++)
{
tot -= used[j + used.size() - used2.size()];
used[j + used.size() - used2.size()] = min(used[j + used.size() - used2.size()] + used2[j], maxx[cur + used2.size() - j - 1]);
tot += used[j + used.size() - used2.size()];
}
}
if (C[x] == c && (tot > best || (tot == best && tot2 < best2)))
{
best = tot, best2 = tot - tot2;
}
bla = tot;
return tot2;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int N, K;
cin >> N >> K;
vector<int> C(N);
vector<vector<int>> colors(K);
for (int i = 0; i < N; i++)
{
cin >> C[i];
colors[C[i]].push_back(i);
}
vector<int> p(N, 0);
vector<vector<int>> AR(N);
for (int i = 1; i < N; i++)
{
cin >> p[i];
AR[p[i]].push_back(i);
}
vector<int> depth(N);
vector<vector<pair<int, int>>> colorpos(K);
dfs(0, AR, depth, 0);
int best = 0, best2 = 0;
for (int i = 0; i < K; i++)
{
for (int j = 0; j < colors[i].size(); j++)
{
colorpos[i].push_back({depth[colors[i][j]], 1});
}
sort(colorpos[i].begin(), colorpos[i].end());
vector<pair<int, int>> temp;
int cur = 0;
for (int j = 0; j < colors[i].size(); j++)
{
if (j > 0 && colorpos[i][j].first != colorpos[i][j - 1].first)
{
temp.push_back({colorpos[i][j - 1].first, cur});
cur = 0;
}
cur++;
}
if (colors[i].size() > 0)
temp.push_back({colorpos[i].back().first, cur});
swap(temp, colorpos[i]);
}
vector<int> available;
map<int, int> M;
dfs2(0, AR, colorpos, available, M, C, colors, 0, best, best2);
for (int i = 0; i < K; i++)
{
if (colors[i].size() >= sqrt(N))
{
vector<int> used, maxx(N);
for (int j = 0; j < colors[i].size(); j++)
maxx[depth[colors[i][j]]]++;
int bla = 0;
int temp = dfs3(0, AR, C, i, used, maxx, 0, best, best2, bla);
}
}
cout << best << ' ' << best2;
}
Compilation message
Main.cpp: In function 'void dfs(int, std::vector<std::vector<int> >&, std::vector<int>&, int)':
Main.cpp:6:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | for (int i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
Main.cpp: In function 'void dfs2(int, std::vector<std::vector<int> >&, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&, std::map<int, int>&, std::vector<int>&, std::vector<std::vector<int> >&, int, int&, int&)':
Main.cpp:17:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
Main.cpp:25:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int j = 0; j < available2.size(); j++)
| ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:39:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 0; i < colorpos[C[x]].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int dfs3(int, std::vector<std::vector<int> >&, std::vector<int>&, int, std::vector<int>&, std::vector<int>&, int, int&, int&, int&)':
Main.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
Main.cpp:73:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int j = 0; j < used2.size(); j++)
| ~~^~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:114:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int j = 0; j < colors[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~
Main.cpp:121:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for (int j = 0; j < colors[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~
Main.cpp:142:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for (int j = 0; j < colors[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~
Main.cpp:145:17: warning: unused variable 'temp' [-Wunused-variable]
145 | int temp = dfs3(0, AR, C, i, used, maxx, 0, best, best2, bla);
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
552 KB |
Output is correct |
3 |
Correct |
1 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 |
5 ms |
1468 KB |
Output is correct |
9 |
Correct |
110 ms |
55876 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
69 ms |
49736 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
1376 KB |
Output is correct |
15 |
Correct |
140 ms |
58292 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1372 KB |
Output is correct |
5 |
Correct |
68 ms |
49728 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
30 ms |
7856 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
75 ms |
28868 KB |
Output is correct |
12 |
Correct |
19 ms |
6112 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
51 ms |
7628 KB |
Output is correct |
16 |
Correct |
51 ms |
7516 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
48 ms |
7680 KB |
Output is correct |
20 |
Correct |
49 ms |
7852 KB |
Output is correct |
21 |
Correct |
51 ms |
7856 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
420 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 |
99 ms |
55508 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
1372 KB |
Output is correct |
11 |
Correct |
113 ms |
58308 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
600 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Incorrect |
109 ms |
17164 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
1368 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
860 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 |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
984 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Incorrect |
2 ms |
776 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
552 KB |
Output is correct |
3 |
Correct |
1 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 |
5 ms |
1468 KB |
Output is correct |
9 |
Correct |
110 ms |
55876 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
69 ms |
49736 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
1376 KB |
Output is correct |
15 |
Correct |
140 ms |
58292 KB |
Output is correct |
16 |
Correct |
1 ms |
348 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 |
1372 KB |
Output is correct |
21 |
Correct |
68 ms |
49728 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
30 ms |
7856 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
75 ms |
28868 KB |
Output is correct |
28 |
Correct |
19 ms |
6112 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
51 ms |
7628 KB |
Output is correct |
32 |
Correct |
51 ms |
7516 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
48 ms |
7680 KB |
Output is correct |
36 |
Correct |
49 ms |
7852 KB |
Output is correct |
37 |
Correct |
51 ms |
7856 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
420 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 |
99 ms |
55508 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
2 ms |
1372 KB |
Output is correct |
49 |
Correct |
113 ms |
58308 KB |
Output is correct |
50 |
Correct |
0 ms |
344 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
2 ms |
600 KB |
Output is correct |
54 |
Correct |
2 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Incorrect |
109 ms |
17164 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |