Submission #1080461

# Submission time Handle Problem Language Result Execution time Memory
1080461 2024-08-29T10:10:12 Z baluteshih Team Coding (EGOI24_teamcoding) C++14
100 / 100
287 ms 29512 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SZ(a) ((int)a.size())
#ifdef bbq
#include <experimental/iterator>
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
    cerr << "\e[1;32m(" << s << ") = (";
    int f = 0;
    (..., (cerr << (f++ ? ", " : "") << a));
    cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
    cerr << "\e[1;33m[" << s << "] = [";
    using namespace experimental;
    copy(L, R, make_ostream_joiner(cerr, ", "));
    cerr << "]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

const int K = 320;
const int MAXN = 100005;

vector<int> G[MAXN], stk, dep, in, out;
int dft;

void dfs(int u, int d) {
    dep[u] = d, in[u] = ++dft;
    stk.pb(u + 1);
    for (int i : G[u])
        dfs(i, d + 1);
    stk.pb(-u - 1);
    out[u] = dft;
}

bool ancestor(int u, int v) {
    return in[u] <= in[v] && out[u] >= out[v];
}

pii operator+(const pii &a, const pii &b) {
    return pii(a.X + b.X, a.Y + b.Y);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> arr(n), pa(n, -1);
    vector<vector<int>> buk(k), layer(k);
    dep.resize(n), in.resize(n), out.resize(n);
    for (int i = 0; i < n; ++i)
        cin >> arr[i], buk[arr[i]].pb(i);
    for (int i = 1; i < n; ++i) {
        cin >> pa[i];
        G[pa[i]].pb(i);
    }
    dfs(0, 0);

    pii ans = pii(0, 0);

    auto proc = [&](pii &cur, int f, int l, int a) {
        debug(f, l, a);
        assert(f >= a && l >= a);
        cur.X += min(f, l);
        cur.Y -= min(f, l) - a;
    };

    auto up = [&](auto &len, int i) {
        if (pa[i] != -1) {
            if (SZ(len[pa[i]]) < SZ(len[i]))
                len[pa[i]].swap(len[i]);
            for (int j = 0; j < SZ(len[i]); ++j)
                len[pa[i]][SZ(len[pa[i]]) - j - 1] = len[i][SZ(len[i]) - j - 1] + len[pa[i]][SZ(len[pa[i]]) - j - 1];
        }
    };

    auto solve_big = [&](int c) {
        vector<vector<pii>> len(n);
        vector<int> f(n);
        for (int i : buk[c])
            ++f[dep[i]];
        int vis = 0;
        for (int i : stk) {
            int flag = (arr[abs(i) - 1] == c);
            if (i > 0) vis += flag;
            else {
                i = -i - 1, vis -= flag;
                len[i].pb(pii(1, flag));
                if (flag) {
                    if (vis == 0) {
                        pii cur = pii(0, 0);
                        for (int j = 0; j < SZ(len[i]); ++j) {
                            int realf = f[dep[i] + j];
                            auto [reall, reala] = len[i][SZ(len[i]) - j - 1];
                            proc(cur, realf, reall, reala);
                        }
                        ans = max(ans, cur);
                    }
                }
                up(len, i);
            }
        }
    };

    vector<int> small, vis(n, -1), cnt(n), ina(n);
    auto solve_small = [&]() {
        vector<vector<int>> len(n);
        for (int c : small) {
            for (int i : buk[c]) {
                if (vis[dep[i]] != c) {
                    vis[dep[i]] = c;
                    layer[c].pb(dep[i]);
                }
            }
            sort(ALL(layer[c]));
        }
        for (int i : stk) {
            if (i > 0) continue;
            i = -i - 1;
            len[i].pb(1);
            debug("exit", i);
            orange(ALL(len[i]));
            if (SZ(buk[arr[i]]) < K) {
                pii cur = pii(0, 0);
                for (int j : layer[arr[i]])
                    cnt[j] = ina[j] = 0;
                for (int j : buk[arr[i]]) {
                    ++cnt[dep[j]];
                    if (ancestor(i, j))
                        ++ina[dep[j]];
                }
                for (int j : layer[arr[i]])
                    if (j >= dep[i]) {
                        if (SZ(len[i]) - 1 >= j - dep[i])
                            proc(cur, cnt[j], len[i][SZ(len[i]) - (j - dep[i]) - 1], ina[j]);
                    }
                ans = max(ans, cur);
            }
            up(len, i);
            if (i != 0) {
                debug("done up", i);
                orange(ALL(len[pa[i]]));
            }
        }
    };

    for (int i = 0; i < k; ++i) {
        if (SZ(buk[i]) >= K) solve_big(i);
        else small.pb(i);
    }
    solve_small();
    cout << ans.X << " " << -ans.Y << "\n";
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:107:34: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  107 |                             auto [reall, reala] = len[i][SZ(len[i]) - j - 1];
      |                                  ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 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 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 3 ms 3164 KB Output is correct
9 Correct 50 ms 26568 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 30 ms 21820 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 3312 KB Output is correct
15 Correct 62 ms 29432 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2620 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 2 ms 3080 KB Output is correct
5 Correct 31 ms 21648 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 3 ms 2908 KB Output is correct
8 Correct 35 ms 13656 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 3072 KB Output is correct
11 Correct 47 ms 17084 KB Output is correct
12 Correct 21 ms 13792 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 46 ms 14028 KB Output is correct
16 Correct 45 ms 14040 KB Output is correct
17 Correct 2 ms 2808 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 45 ms 14188 KB Output is correct
20 Correct 52 ms 14148 KB Output is correct
21 Correct 45 ms 13964 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 2 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2804 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 50 ms 26532 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 3 ms 3164 KB Output is correct
11 Correct 58 ms 29512 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 3164 KB Output is correct
18 Correct 62 ms 20820 KB Output is correct
19 Correct 64 ms 18648 KB Output is correct
20 Correct 58 ms 16952 KB Output is correct
21 Correct 63 ms 15276 KB Output is correct
22 Correct 69 ms 22468 KB Output is correct
23 Correct 57 ms 22700 KB Output is correct
24 Correct 52 ms 19812 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 2 ms 3164 KB Output is correct
27 Correct 2 ms 2908 KB Output is correct
28 Correct 2 ms 3028 KB Output is correct
29 Correct 6 ms 4700 KB Output is correct
30 Correct 62 ms 18972 KB Output is correct
31 Correct 69 ms 21704 KB Output is correct
32 Correct 61 ms 23756 KB Output is correct
33 Correct 60 ms 21056 KB Output is correct
34 Correct 1 ms 2648 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 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 2652 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 2 ms 3164 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 2 ms 3164 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 3164 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 2 ms 2908 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2648 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 3064 KB Output is correct
25 Correct 2 ms 3164 KB Output is correct
26 Correct 2 ms 2908 KB Output is correct
27 Correct 2 ms 3164 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 2 ms 3164 KB Output is correct
30 Correct 2 ms 3164 KB Output is correct
31 Correct 2 ms 2904 KB Output is correct
32 Correct 2 ms 2904 KB Output is correct
33 Correct 2 ms 3072 KB Output is correct
34 Correct 3 ms 2908 KB Output is correct
35 Correct 2 ms 2908 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 2 ms 2908 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 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 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 3 ms 3164 KB Output is correct
9 Correct 50 ms 26568 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 30 ms 21820 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 3312 KB Output is correct
15 Correct 62 ms 29432 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2620 KB Output is correct
19 Correct 1 ms 2648 KB Output is correct
20 Correct 2 ms 3080 KB Output is correct
21 Correct 31 ms 21648 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 3 ms 2908 KB Output is correct
24 Correct 35 ms 13656 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 3072 KB Output is correct
27 Correct 47 ms 17084 KB Output is correct
28 Correct 21 ms 13792 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 46 ms 14028 KB Output is correct
32 Correct 45 ms 14040 KB Output is correct
33 Correct 2 ms 2808 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 45 ms 14188 KB Output is correct
36 Correct 52 ms 14148 KB Output is correct
37 Correct 45 ms 13964 KB Output is correct
38 Correct 2 ms 2652 KB Output is correct
39 Correct 1 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 1 ms 2652 KB Output is correct
42 Correct 2 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 1 ms 2804 KB Output is correct
45 Correct 2 ms 2652 KB Output is correct
46 Correct 50 ms 26532 KB Output is correct
47 Correct 1 ms 2648 KB Output is correct
48 Correct 3 ms 3164 KB Output is correct
49 Correct 58 ms 29512 KB Output is correct
50 Correct 2 ms 2652 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 1 ms 2652 KB Output is correct
53 Correct 2 ms 2908 KB Output is correct
54 Correct 2 ms 2908 KB Output is correct
55 Correct 2 ms 3164 KB Output is correct
56 Correct 62 ms 20820 KB Output is correct
57 Correct 64 ms 18648 KB Output is correct
58 Correct 58 ms 16952 KB Output is correct
59 Correct 63 ms 15276 KB Output is correct
60 Correct 69 ms 22468 KB Output is correct
61 Correct 57 ms 22700 KB Output is correct
62 Correct 52 ms 19812 KB Output is correct
63 Correct 2 ms 2908 KB Output is correct
64 Correct 2 ms 3164 KB Output is correct
65 Correct 2 ms 2908 KB Output is correct
66 Correct 2 ms 3028 KB Output is correct
67 Correct 6 ms 4700 KB Output is correct
68 Correct 62 ms 18972 KB Output is correct
69 Correct 69 ms 21704 KB Output is correct
70 Correct 61 ms 23756 KB Output is correct
71 Correct 60 ms 21056 KB Output is correct
72 Correct 1 ms 2648 KB Output is correct
73 Correct 1 ms 2652 KB Output is correct
74 Correct 1 ms 2652 KB Output is correct
75 Correct 1 ms 2652 KB Output is correct
76 Correct 2 ms 2652 KB Output is correct
77 Correct 2 ms 2652 KB Output is correct
78 Correct 1 ms 2652 KB Output is correct
79 Correct 1 ms 2652 KB Output is correct
80 Correct 1 ms 2652 KB Output is correct
81 Correct 1 ms 2648 KB Output is correct
82 Correct 2 ms 2648 KB Output is correct
83 Correct 2 ms 3164 KB Output is correct
84 Correct 2 ms 2652 KB Output is correct
85 Correct 2 ms 3164 KB Output is correct
86 Correct 2 ms 2652 KB Output is correct
87 Correct 2 ms 3164 KB Output is correct
88 Correct 1 ms 2652 KB Output is correct
89 Correct 2 ms 2908 KB Output is correct
90 Correct 1 ms 2652 KB Output is correct
91 Correct 2 ms 2908 KB Output is correct
92 Correct 1 ms 2652 KB Output is correct
93 Correct 1 ms 2652 KB Output is correct
94 Correct 2 ms 2648 KB Output is correct
95 Correct 2 ms 2908 KB Output is correct
96 Correct 1 ms 2652 KB Output is correct
97 Correct 1 ms 2652 KB Output is correct
98 Correct 2 ms 2908 KB Output is correct
99 Correct 2 ms 3064 KB Output is correct
100 Correct 2 ms 3164 KB Output is correct
101 Correct 2 ms 2908 KB Output is correct
102 Correct 2 ms 3164 KB Output is correct
103 Correct 2 ms 2908 KB Output is correct
104 Correct 2 ms 3164 KB Output is correct
105 Correct 2 ms 3164 KB Output is correct
106 Correct 2 ms 2904 KB Output is correct
107 Correct 2 ms 2904 KB Output is correct
108 Correct 2 ms 3072 KB Output is correct
109 Correct 3 ms 2908 KB Output is correct
110 Correct 2 ms 2908 KB Output is correct
111 Correct 2 ms 2908 KB Output is correct
112 Correct 2 ms 2908 KB Output is correct
113 Correct 1 ms 2652 KB Output is correct
114 Correct 1 ms 2652 KB Output is correct
115 Correct 1 ms 2652 KB Output is correct
116 Correct 1 ms 2652 KB Output is correct
117 Correct 2 ms 2648 KB Output is correct
118 Correct 1 ms 2652 KB Output is correct
119 Correct 1 ms 2652 KB Output is correct
120 Correct 1 ms 2652 KB Output is correct
121 Correct 1 ms 2652 KB Output is correct
122 Correct 1 ms 2652 KB Output is correct
123 Correct 1 ms 2652 KB Output is correct
124 Correct 3 ms 3164 KB Output is correct
125 Correct 50 ms 26492 KB Output is correct
126 Correct 2 ms 2652 KB Output is correct
127 Correct 2 ms 3076 KB Output is correct
128 Correct 30 ms 21680 KB Output is correct
129 Correct 2 ms 2652 KB Output is correct
130 Correct 2 ms 3164 KB Output is correct
131 Correct 54 ms 29356 KB Output is correct
132 Correct 1 ms 2652 KB Output is correct
133 Correct 2 ms 2908 KB Output is correct
134 Correct 29 ms 13764 KB Output is correct
135 Correct 2 ms 2652 KB Output is correct
136 Correct 2 ms 2964 KB Output is correct
137 Correct 46 ms 17092 KB Output is correct
138 Correct 23 ms 13536 KB Output is correct
139 Correct 1 ms 2652 KB Output is correct
140 Correct 1 ms 2652 KB Output is correct
141 Correct 46 ms 14100 KB Output is correct
142 Correct 48 ms 14252 KB Output is correct
143 Correct 2 ms 2648 KB Output is correct
144 Correct 2 ms 2908 KB Output is correct
145 Correct 45 ms 13996 KB Output is correct
146 Correct 47 ms 14028 KB Output is correct
147 Correct 49 ms 14188 KB Output is correct
148 Correct 1 ms 2648 KB Output is correct
149 Correct 1 ms 2648 KB Output is correct
150 Correct 2 ms 3080 KB Output is correct
151 Correct 2 ms 3076 KB Output is correct
152 Correct 2 ms 3160 KB Output is correct
153 Correct 76 ms 20920 KB Output is correct
154 Correct 59 ms 18636 KB Output is correct
155 Correct 57 ms 16900 KB Output is correct
156 Correct 57 ms 15340 KB Output is correct
157 Correct 63 ms 22540 KB Output is correct
158 Correct 59 ms 22724 KB Output is correct
159 Correct 51 ms 19652 KB Output is correct
160 Correct 2 ms 2908 KB Output is correct
161 Correct 2 ms 3164 KB Output is correct
162 Correct 2 ms 2908 KB Output is correct
163 Correct 2 ms 3164 KB Output is correct
164 Correct 7 ms 4700 KB Output is correct
165 Correct 55 ms 18772 KB Output is correct
166 Correct 70 ms 21884 KB Output is correct
167 Correct 63 ms 23756 KB Output is correct
168 Correct 58 ms 20940 KB Output is correct
169 Correct 2 ms 3160 KB Output is correct
170 Correct 2 ms 2904 KB Output is correct
171 Correct 2 ms 2812 KB Output is correct
172 Correct 2 ms 2908 KB Output is correct
173 Correct 2 ms 2908 KB Output is correct
174 Correct 2 ms 2908 KB Output is correct
175 Correct 3 ms 2788 KB Output is correct
176 Correct 2 ms 2908 KB Output is correct
177 Correct 63 ms 24996 KB Output is correct
178 Correct 287 ms 14972 KB Output is correct
179 Correct 57 ms 18352 KB Output is correct
180 Correct 5 ms 4188 KB Output is correct
181 Correct 47 ms 19748 KB Output is correct
182 Correct 7 ms 3952 KB Output is correct
183 Correct 7 ms 3952 KB Output is correct
184 Correct 7 ms 4208 KB Output is correct
185 Correct 6 ms 3948 KB Output is correct
186 Correct 76 ms 18372 KB Output is correct
187 Correct 56 ms 15324 KB Output is correct
188 Correct 87 ms 16436 KB Output is correct
189 Correct 54 ms 15012 KB Output is correct
190 Correct 65 ms 15780 KB Output is correct
191 Correct 48 ms 17172 KB Output is correct
192 Correct 6 ms 3952 KB Output is correct
193 Correct 51 ms 14988 KB Output is correct
194 Correct 33 ms 16072 KB Output is correct
195 Correct 2 ms 2648 KB Output is correct
196 Correct 1 ms 2652 KB Output is correct
197 Correct 1 ms 2652 KB Output is correct
198 Correct 1 ms 2652 KB Output is correct