Submission #1043198

# Submission time Handle Problem Language Result Execution time Memory
1043198 2024-08-04T04:48:36 Z model_code Team Coding (EGOI24_teamcoding) C++17
100 / 100
934 ms 203680 KB
#include<bits/stdc++.h>

using namespace std;
typedef vector<int> vi;

#define sz(x) (int)x.size()

  
void solve(int v, vector<vi>& adj,vi& col,vi& col_count,int c, vi& levels,int d, vi& vis){
      vis[v]=1;
      if(d==sz(col_count)) col_count.push_back(0);
      if(col[v]==c) col_count[d]++;
      if(d==sz(levels)) levels.push_back(0);
      levels[d]++;  
      for(int w:adj[v]){
          solve(w,adj,col,col_count,c,levels,d+1,vis);
      }
}


void dfs(int v, int p,vector<vi>& adj, vi& level,vi& order){
    if(v!= 0)
        level[v]=level[p]+1;
    for(int w:adj[v]){
        dfs(w,v,adj,level,order);
    }
    order.push_back(v);
}

void col_tree(int v, int p, vector<vi>& adj, vi& col, vector<vi>& col_adj, vi& cur_p,vector<deque<int>>& col_root) {
    if(cur_p[col[v]] == -1) {
        col_root[col[v]].push_back(v);
    } else {
        col_adj[cur_p[col[v]]].push_back(v);
    }
    int old = cur_p[col[v]];
    cur_p[col[v]] = v;
    for(int w:adj[v]){
        if(w==p) continue;
        col_tree(w,v,adj,col,col_adj,cur_p,col_root);
    }
    cur_p[col[v]] = old;
}


signed main(){
    cin.tie(0)-> sync_with_stdio(false);
    int n,k;
    cin >> n >> k;
    int split = 300;
    vi col(n);
    vector<vi> col_v(k);
    vi col_size(k,0);
    for(int i=0;i<n;++i){
        cin >> col[i];
        col_v[col[i]].push_back(i);
        col_size[col[i]]++;
    }
    
    vector<vi> adj(n);
    for(int i=0;i<n-1;++i){
        int u,v;
        u = i+1;
        cin >> v;
        adj[v].push_back(u);
    }
    vi order;
    vi v_level(n,0);
    dfs(0,-1,adj,v_level,order);        

    vector<vi> col_adj(n);
    vector<deque<int>> col_root(k);
    vi col_p(k,-1);

    col_tree(0,-1,adj,col,col_adj, col_p, col_root);
    cerr << "coltree solved" <<'\n';

    vector<unordered_map<int,int>> col_count(k);
    for(int i=0;i<n;++i){
        auto  search = col_count[col[i]].find(v_level[i]);
        if(search == col_count[col[i]].end()) col_count[col[i]][v_level[i]]=1;
        else search->second++;
    }

    int val = 0;
    int swaps = 0;
    
    cerr << " starting" << '\n';
    vector<deque<int>>  counts(n);        
    for(int v: order){
        for(int w:adj[v]){
            if(counts[w].size() > counts[v].size()) swap(counts[w],counts[v]);
            for(int i=0;i<sz(counts[w]);++i){
                counts[v][i]+=counts[w][i];
            }
        }
        counts[v].push_front(1);
        if (col_size[col[v]] < split) {
            //if v is not color root, skip it
            if(v != col_root[col[v]].front()) continue;
            col_root[col[v]].pop_front();
        
            int v_val = 0;
            int v_swaps = 0;
            unordered_map<int,int> col_lev;
            unordered_set<int> vis;
            queue<int> q;
            q.push(v);

            while(!q.empty()){
                int u = q.front();
                q.pop();
                if(v_level[u]<v_level[v]){
                    assert(false);
                }
                auto  search = col_lev.find(v_level[u]);
                if(search == col_lev.end()) col_lev[v_level[u]]=1;
                else search->second++;
                for(int w:col_adj[u]) q.push(w);
            }
            for(int u : col_v[col[v]]){
                int d = v_level[u];
                if(vis.find(d)!=vis.end()) continue;
                vis.insert(d);
                if(d<v_level[v] || d > v_level[v]+sz(counts[v])-1) continue;
                int d_val= min(counts[v][d-v_level[v]],col_count[col[v]][d]);
                v_val += d_val;
                v_swaps += d_val-col_lev[d];
            }
            
            if(v_val>val){
                val=v_val;
                swaps=v_swaps;
            }
            if(val==v_val)
                swaps = min(v_swaps,swaps);
        }
    }
    cerr<< "starting heavy" <<'\n';           
    for(int i=0;i<k;++i){
        if(col_size[i]< split) continue;
        vi vis(n,0);
        for(int j=0;j< sz(col_root[i]);++j){
            int v = col_root[i][j];
            vi v_col;
            vi v_lev;
            solve(v,adj,col,v_col,col[v],v_lev,0,vis);
            v_lev.push_back(0);
            int v_val = 0;
            int v_swaps=0;
            int d = 0;
            while(v_lev[d]>0){
                 int add = min(v_lev[d],col_count[col[v]][d+v_level[v]]);
                 v_val += add;
                 v_swaps += add-v_col[d];
                 ++d;
            }
            if(v_val>val){
                val=v_val;
                swaps=v_swaps;
            }
            if(val==v_val)
                    swaps = min(v_swaps,swaps);
        }
    } 
    cout << val << ' ' << swaps<<'\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 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 2 ms 2348 KB Output is correct
9 Correct 158 ms 143916 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 65 ms 102748 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 3 ms 3996 KB Output is correct
15 Correct 168 ms 176844 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 73 ms 102920 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 97 ms 104184 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 129 ms 115136 KB Output is correct
12 Correct 76 ms 127192 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 103 ms 104036 KB Output is correct
16 Correct 104 ms 104016 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 112 ms 104160 KB Output is correct
20 Correct 114 ms 104132 KB Output is correct
21 Correct 110 ms 104136 KB Output is correct
22 Correct 0 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 176 ms 144044 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 5 ms 3752 KB Output is correct
11 Correct 191 ms 176712 KB Output is correct
12 Correct 1 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 4 ms 3420 KB Output is correct
16 Correct 3 ms 2648 KB Output is correct
17 Correct 4 ms 4188 KB Output is correct
18 Correct 282 ms 170520 KB Output is correct
19 Correct 314 ms 141068 KB Output is correct
20 Correct 199 ms 102452 KB Output is correct
21 Correct 245 ms 126800 KB Output is correct
22 Correct 298 ms 203576 KB Output is correct
23 Correct 262 ms 131788 KB Output is correct
24 Correct 253 ms 112500 KB Output is correct
25 Correct 4 ms 2464 KB Output is correct
26 Correct 3 ms 3420 KB Output is correct
27 Correct 6 ms 3676 KB Output is correct
28 Correct 7 ms 4188 KB Output is correct
29 Correct 23 ms 18664 KB Output is correct
30 Correct 266 ms 151144 KB Output is correct
31 Correct 319 ms 174800 KB Output is correct
32 Correct 297 ms 173096 KB Output is correct
33 Correct 302 ms 183292 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 1 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 452 KB Output is correct
6 Correct 0 ms 424 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 1 ms 456 KB Output is correct
10 Correct 2 ms 2392 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 3 ms 3800 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 2 ms 2640 KB Output is correct
17 Correct 1 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 3 ms 2396 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 5 ms 3416 KB Output is correct
24 Correct 4 ms 2652 KB Output is correct
25 Correct 6 ms 4184 KB Output is correct
26 Correct 3 ms 2676 KB Output is correct
27 Correct 4 ms 3420 KB Output is correct
28 Correct 4 ms 3620 KB Output is correct
29 Correct 6 ms 4188 KB Output is correct
30 Correct 5 ms 4188 KB Output is correct
31 Correct 8 ms 2140 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 3 ms 2648 KB Output is correct
34 Correct 2 ms 3676 KB Output is correct
35 Correct 3 ms 4188 KB Output is correct
36 Correct 3 ms 2652 KB Output is correct
37 Correct 3 ms 3676 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 344 KB Output is correct
40 Correct 1 ms 348 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 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 2 ms 2348 KB Output is correct
9 Correct 158 ms 143916 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 65 ms 102748 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 3 ms 3996 KB Output is correct
15 Correct 168 ms 176844 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 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 73 ms 102920 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 2396 KB Output is correct
24 Correct 97 ms 104184 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 129 ms 115136 KB Output is correct
28 Correct 76 ms 127192 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 103 ms 104036 KB Output is correct
32 Correct 104 ms 104016 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 112 ms 104160 KB Output is correct
36 Correct 114 ms 104132 KB Output is correct
37 Correct 110 ms 104136 KB Output is correct
38 Correct 0 ms 344 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 1 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 604 KB Output is correct
46 Correct 176 ms 144044 KB Output is correct
47 Correct 0 ms 604 KB Output is correct
48 Correct 5 ms 3752 KB Output is correct
49 Correct 191 ms 176712 KB Output is correct
50 Correct 1 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 4 ms 3420 KB Output is correct
54 Correct 3 ms 2648 KB Output is correct
55 Correct 4 ms 4188 KB Output is correct
56 Correct 282 ms 170520 KB Output is correct
57 Correct 314 ms 141068 KB Output is correct
58 Correct 199 ms 102452 KB Output is correct
59 Correct 245 ms 126800 KB Output is correct
60 Correct 298 ms 203576 KB Output is correct
61 Correct 262 ms 131788 KB Output is correct
62 Correct 253 ms 112500 KB Output is correct
63 Correct 4 ms 2464 KB Output is correct
64 Correct 3 ms 3420 KB Output is correct
65 Correct 6 ms 3676 KB Output is correct
66 Correct 7 ms 4188 KB Output is correct
67 Correct 23 ms 18664 KB Output is correct
68 Correct 266 ms 151144 KB Output is correct
69 Correct 319 ms 174800 KB Output is correct
70 Correct 297 ms 173096 KB Output is correct
71 Correct 302 ms 183292 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 1 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 452 KB Output is correct
81 Correct 0 ms 424 KB Output is correct
82 Correct 0 ms 604 KB Output is correct
83 Correct 3 ms 2396 KB Output is correct
84 Correct 1 ms 456 KB Output is correct
85 Correct 2 ms 2392 KB Output is correct
86 Correct 0 ms 604 KB Output is correct
87 Correct 3 ms 3800 KB Output is correct
88 Correct 1 ms 348 KB Output is correct
89 Correct 2 ms 2396 KB Output is correct
90 Correct 1 ms 348 KB Output is correct
91 Correct 2 ms 2640 KB Output is correct
92 Correct 1 ms 348 KB Output is correct
93 Correct 0 ms 348 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 3 ms 2396 KB Output is correct
96 Correct 0 ms 344 KB Output is correct
97 Correct 1 ms 348 KB Output is correct
98 Correct 5 ms 3416 KB Output is correct
99 Correct 4 ms 2652 KB Output is correct
100 Correct 6 ms 4184 KB Output is correct
101 Correct 3 ms 2676 KB Output is correct
102 Correct 4 ms 3420 KB Output is correct
103 Correct 4 ms 3620 KB Output is correct
104 Correct 6 ms 4188 KB Output is correct
105 Correct 5 ms 4188 KB Output is correct
106 Correct 8 ms 2140 KB Output is correct
107 Correct 2 ms 2652 KB Output is correct
108 Correct 3 ms 2648 KB Output is correct
109 Correct 2 ms 3676 KB Output is correct
110 Correct 3 ms 4188 KB Output is correct
111 Correct 3 ms 2652 KB Output is correct
112 Correct 3 ms 3676 KB Output is correct
113 Correct 0 ms 348 KB Output is correct
114 Correct 0 ms 344 KB Output is correct
115 Correct 1 ms 348 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 1 ms 344 KB Output is correct
120 Correct 0 ms 348 KB Output is correct
121 Correct 0 ms 348 KB Output is correct
122 Correct 1 ms 348 KB Output is correct
123 Correct 0 ms 604 KB Output is correct
124 Correct 2 ms 2396 KB Output is correct
125 Correct 197 ms 144076 KB Output is correct
126 Correct 0 ms 348 KB Output is correct
127 Correct 2 ms 2396 KB Output is correct
128 Correct 80 ms 102896 KB Output is correct
129 Correct 0 ms 604 KB Output is correct
130 Correct 3 ms 3932 KB Output is correct
131 Correct 199 ms 176716 KB Output is correct
132 Correct 0 ms 348 KB Output is correct
133 Correct 3 ms 2396 KB Output is correct
134 Correct 99 ms 104276 KB Output is correct
135 Correct 1 ms 348 KB Output is correct
136 Correct 3 ms 2628 KB Output is correct
137 Correct 149 ms 115184 KB Output is correct
138 Correct 83 ms 127192 KB Output is correct
139 Correct 0 ms 344 KB Output is correct
140 Correct 0 ms 348 KB Output is correct
141 Correct 124 ms 103960 KB Output is correct
142 Correct 121 ms 103944 KB Output is correct
143 Correct 1 ms 344 KB Output is correct
144 Correct 2 ms 2396 KB Output is correct
145 Correct 117 ms 104080 KB Output is correct
146 Correct 131 ms 104088 KB Output is correct
147 Correct 103 ms 104080 KB Output is correct
148 Correct 0 ms 348 KB Output is correct
149 Correct 1 ms 348 KB Output is correct
150 Correct 4 ms 3416 KB Output is correct
151 Correct 3 ms 2652 KB Output is correct
152 Correct 4 ms 4188 KB Output is correct
153 Correct 307 ms 170620 KB Output is correct
154 Correct 287 ms 141260 KB Output is correct
155 Correct 250 ms 102600 KB Output is correct
156 Correct 217 ms 126384 KB Output is correct
157 Correct 310 ms 203680 KB Output is correct
158 Correct 228 ms 131788 KB Output is correct
159 Correct 255 ms 112328 KB Output is correct
160 Correct 3 ms 2652 KB Output is correct
161 Correct 2 ms 3420 KB Output is correct
162 Correct 3 ms 3676 KB Output is correct
163 Correct 4 ms 4028 KB Output is correct
164 Correct 19 ms 18776 KB Output is correct
165 Correct 305 ms 151124 KB Output is correct
166 Correct 334 ms 174828 KB Output is correct
167 Correct 310 ms 173004 KB Output is correct
168 Correct 311 ms 183332 KB Output is correct
169 Correct 3 ms 4188 KB Output is correct
170 Correct 8 ms 2152 KB Output is correct
171 Correct 2 ms 2652 KB Output is correct
172 Correct 2 ms 2652 KB Output is correct
173 Correct 2 ms 3676 KB Output is correct
174 Correct 3 ms 4188 KB Output is correct
175 Correct 4 ms 2652 KB Output is correct
176 Correct 3 ms 3676 KB Output is correct
177 Correct 273 ms 194516 KB Output is correct
178 Correct 934 ms 84288 KB Output is correct
179 Correct 271 ms 159440 KB Output is correct
180 Correct 13 ms 14700 KB Output is correct
181 Correct 144 ms 156496 KB Output is correct
182 Correct 8 ms 14172 KB Output is correct
183 Correct 11 ms 15720 KB Output is correct
184 Correct 15 ms 17756 KB Output is correct
185 Correct 9 ms 13612 KB Output is correct
186 Correct 161 ms 183608 KB Output is correct
187 Correct 110 ms 146240 KB Output is correct
188 Correct 149 ms 153604 KB Output is correct
189 Correct 115 ms 142324 KB Output is correct
190 Correct 132 ms 150396 KB Output is correct
191 Correct 152 ms 174096 KB Output is correct
192 Correct 12 ms 14168 KB Output is correct
193 Correct 137 ms 115260 KB Output is correct
194 Correct 113 ms 110796 KB Output is correct
195 Correct 0 ms 348 KB Output is correct
196 Correct 0 ms 348 KB Output is correct
197 Correct 0 ms 344 KB Output is correct
198 Correct 0 ms 348 KB Output is correct