Submission #1043940

# Submission time Handle Problem Language Result Execution time Memory
1043940 2024-08-05T04:52:56 Z ㅇㅇ(#11061) Team Coding (EGOI24_teamcoding) C++17
100 / 100
2056 ms 248028 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll=long long;
using db=double;
using pii=array<int,2>;
const int N=1e5+5,B=300;
int n,k,a[N],cnt[N],p[N],in[N],out[N],C,dep[N],lim[N];
bool qry[N];
pii ans;
vector<int> l[N],c[N];
deque<pii> V[N];
deque<int> W[N];
int val[N],cost[N],cur[N],cur2[N];
void dfs(int u){
	in[u]=++C;
	for(int v: c[u]){
		dep[v]=dep[u]+1;
		dfs(v);
	}
	out[u]=C;
}
void mrg(int u,int v){
	if(V[u].size()<V[v].size()){
		swap(V[u],V[v]);
		swap(val[u],val[v]);
		swap(cost[u],cost[v]);
	}
	for(int i=0;i<(int)V[v].size();i++){
		val[u]-=min(lim[dep[u]+i+1],V[u][i][0]);
		cost[u]-=min(lim[dep[u]+i+1],V[u][i][0])-V[u][i][1];
		V[u][i][0]+=V[v][i][0];
		V[u][i][1]+=V[v][i][1];
		val[u]+=min(lim[dep[u]+i+1],V[u][i][0]);
		cost[u]+=min(lim[dep[u]+i+1],V[u][i][0])-V[u][i][1];
	}
}
void dfs2(int u,int col){
	val[u]=cost[u]=0;
	V[u].clear();
	for(int v: c[u]){
		dfs2(v,col);
		mrg(u,v);
	}
	V[u].push_front({1,a[u]==col});
	debug(u,val[u],cost[u],V[u]);
	if(lim[dep[u]]){
		val[u]++;
		if(a[u]!=col) cost[u]++;
	}
	if(a[u]==col) ans=max(ans,{val[u],-cost[u]});
}
void solve(int col){
	for(int i=1;i<=n;i++) lim[i]=0;
	for(int u: l[col]) lim[dep[u]]++;
	dfs2(1,col);
}
void mrg2(int u,int v){
	if(W[u].size()<W[v].size()) swap(W[u],W[v]);
	for(int i=0;i<(int)W[v].size();i++) W[u][i]+=W[v][i];
}
void dfs3(int u){
	for(int v: c[u]){
		dfs3(v);
		mrg2(u,v);
	}
	W[u].push_front(1);
	if(cnt[a[u]]<B){
		int res=0,cnt=0;
		for(int v: l[a[u]]) if(dep[u]<=dep[v]&&dep[v]-dep[u]<(int)W[u].size()){
			res-=min(W[u][dep[v]-dep[u]],cur[dep[v]]);
			cnt-=min(W[u][dep[v]-dep[u]],cur[dep[v]])-cur2[dep[v]];
			cur[dep[v]]++;
			if(in[u]<=in[v]&&in[v]<=out[u]) cur2[dep[v]]++;
			res+=min(W[u][dep[v]-dep[u]],cur[dep[v]]);
			cnt+=min(W[u][dep[v]-dep[u]],cur[dep[v]])-cur2[dep[v]];
		}
		ans=max(ans,{res,-cnt});
		for(int v: l[a[u]]) cur[dep[v]]=cur2[dep[v]]=0;
	}
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i]++;
		cnt[a[i]]++;
		l[a[i]].push_back(i);
	}
	for(int i=2;i<=n;i++){
		cin>>p[i];
		p[i]++;
		c[p[i]].push_back(i);
	}
	dfs(1);
	for(int i=1;i<=k;i++) if(cnt[i]>=B) solve(i);
	dfs3(1);
	ans[1]=-ans[1];
	cout<<ans[0]<<" "<<ans[1];
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 55 ms 141908 KB Output is correct
2 Correct 55 ms 141780 KB Output is correct
3 Correct 51 ms 141904 KB Output is correct
4 Correct 50 ms 141880 KB Output is correct
5 Correct 58 ms 141908 KB Output is correct
6 Correct 74 ms 141968 KB Output is correct
7 Correct 54 ms 141940 KB Output is correct
8 Correct 59 ms 142356 KB Output is correct
9 Correct 133 ms 159832 KB Output is correct
10 Correct 57 ms 141904 KB Output is correct
11 Correct 48 ms 142352 KB Output is correct
12 Correct 86 ms 159180 KB Output is correct
13 Correct 53 ms 141908 KB Output is correct
14 Correct 60 ms 142172 KB Output is correct
15 Correct 98 ms 160596 KB Output is correct
16 Correct 57 ms 141908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 141924 KB Output is correct
2 Correct 54 ms 141904 KB Output is correct
3 Correct 52 ms 141828 KB Output is correct
4 Correct 53 ms 142344 KB Output is correct
5 Correct 80 ms 159204 KB Output is correct
6 Correct 61 ms 141908 KB Output is correct
7 Correct 61 ms 142928 KB Output is correct
8 Correct 163 ms 197248 KB Output is correct
9 Correct 61 ms 141908 KB Output is correct
10 Correct 65 ms 142932 KB Output is correct
11 Correct 199 ms 190156 KB Output is correct
12 Correct 127 ms 247628 KB Output is correct
13 Correct 59 ms 141864 KB Output is correct
14 Correct 59 ms 141904 KB Output is correct
15 Correct 186 ms 186200 KB Output is correct
16 Correct 189 ms 186400 KB Output is correct
17 Correct 57 ms 141904 KB Output is correct
18 Correct 63 ms 142724 KB Output is correct
19 Correct 189 ms 186096 KB Output is correct
20 Correct 200 ms 186056 KB Output is correct
21 Correct 194 ms 186056 KB Output is correct
22 Correct 52 ms 141912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 142012 KB Output is correct
2 Correct 52 ms 141908 KB Output is correct
3 Correct 54 ms 141904 KB Output is correct
4 Correct 54 ms 141908 KB Output is correct
5 Correct 54 ms 141908 KB Output is correct
6 Correct 55 ms 141908 KB Output is correct
7 Correct 55 ms 141856 KB Output is correct
8 Correct 96 ms 160080 KB Output is correct
9 Correct 61 ms 141956 KB Output is correct
10 Correct 55 ms 142252 KB Output is correct
11 Correct 94 ms 160592 KB Output is correct
12 Correct 50 ms 141904 KB Output is correct
13 Correct 51 ms 141912 KB Output is correct
14 Correct 55 ms 141812 KB Output is correct
15 Correct 58 ms 142428 KB Output is correct
16 Correct 53 ms 142424 KB Output is correct
17 Correct 63 ms 142676 KB Output is correct
18 Correct 156 ms 173148 KB Output is correct
19 Correct 218 ms 165016 KB Output is correct
20 Correct 117 ms 158444 KB Output is correct
21 Correct 149 ms 184148 KB Output is correct
22 Correct 136 ms 190472 KB Output is correct
23 Correct 119 ms 155476 KB Output is correct
24 Correct 117 ms 153232 KB Output is correct
25 Correct 54 ms 142164 KB Output is correct
26 Correct 58 ms 142416 KB Output is correct
27 Correct 56 ms 142416 KB Output is correct
28 Correct 66 ms 142672 KB Output is correct
29 Correct 61 ms 145232 KB Output is correct
30 Correct 131 ms 173648 KB Output is correct
31 Correct 126 ms 166740 KB Output is correct
32 Correct 127 ms 159680 KB Output is correct
33 Correct 161 ms 185428 KB Output is correct
34 Correct 58 ms 141832 KB Output is correct
35 Correct 58 ms 142004 KB Output is correct
36 Correct 56 ms 141908 KB Output is correct
37 Correct 57 ms 141840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 141904 KB Output is correct
2 Correct 49 ms 141908 KB Output is correct
3 Correct 57 ms 142012 KB Output is correct
4 Correct 55 ms 141904 KB Output is correct
5 Correct 55 ms 141920 KB Output is correct
6 Correct 54 ms 142016 KB Output is correct
7 Correct 54 ms 141904 KB Output is correct
8 Correct 56 ms 142160 KB Output is correct
9 Correct 46 ms 141904 KB Output is correct
10 Correct 51 ms 142208 KB Output is correct
11 Correct 52 ms 141904 KB Output is correct
12 Correct 65 ms 142200 KB Output is correct
13 Correct 65 ms 141928 KB Output is correct
14 Correct 57 ms 143000 KB Output is correct
15 Correct 55 ms 142044 KB Output is correct
16 Correct 59 ms 142932 KB Output is correct
17 Correct 59 ms 141908 KB Output is correct
18 Correct 69 ms 141908 KB Output is correct
19 Correct 58 ms 141904 KB Output is correct
20 Correct 61 ms 142728 KB Output is correct
21 Correct 58 ms 141908 KB Output is correct
22 Correct 57 ms 141816 KB Output is correct
23 Correct 53 ms 142648 KB Output is correct
24 Correct 61 ms 142416 KB Output is correct
25 Correct 63 ms 142860 KB Output is correct
26 Correct 56 ms 142164 KB Output is correct
27 Correct 64 ms 142416 KB Output is correct
28 Correct 58 ms 142672 KB Output is correct
29 Correct 62 ms 142672 KB Output is correct
30 Correct 58 ms 143184 KB Output is correct
31 Correct 57 ms 142164 KB Output is correct
32 Correct 61 ms 142724 KB Output is correct
33 Correct 61 ms 142420 KB Output is correct
34 Correct 66 ms 143032 KB Output is correct
35 Correct 63 ms 142928 KB Output is correct
36 Correct 58 ms 142676 KB Output is correct
37 Correct 63 ms 142760 KB Output is correct
38 Correct 55 ms 142000 KB Output is correct
39 Correct 55 ms 141908 KB Output is correct
40 Correct 56 ms 141908 KB Output is correct
41 Correct 57 ms 141832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 141908 KB Output is correct
2 Correct 55 ms 141780 KB Output is correct
3 Correct 51 ms 141904 KB Output is correct
4 Correct 50 ms 141880 KB Output is correct
5 Correct 58 ms 141908 KB Output is correct
6 Correct 74 ms 141968 KB Output is correct
7 Correct 54 ms 141940 KB Output is correct
8 Correct 59 ms 142356 KB Output is correct
9 Correct 133 ms 159832 KB Output is correct
10 Correct 57 ms 141904 KB Output is correct
11 Correct 48 ms 142352 KB Output is correct
12 Correct 86 ms 159180 KB Output is correct
13 Correct 53 ms 141908 KB Output is correct
14 Correct 60 ms 142172 KB Output is correct
15 Correct 98 ms 160596 KB Output is correct
16 Correct 57 ms 141908 KB Output is correct
17 Correct 54 ms 141924 KB Output is correct
18 Correct 54 ms 141904 KB Output is correct
19 Correct 52 ms 141828 KB Output is correct
20 Correct 53 ms 142344 KB Output is correct
21 Correct 80 ms 159204 KB Output is correct
22 Correct 61 ms 141908 KB Output is correct
23 Correct 61 ms 142928 KB Output is correct
24 Correct 163 ms 197248 KB Output is correct
25 Correct 61 ms 141908 KB Output is correct
26 Correct 65 ms 142932 KB Output is correct
27 Correct 199 ms 190156 KB Output is correct
28 Correct 127 ms 247628 KB Output is correct
29 Correct 59 ms 141864 KB Output is correct
30 Correct 59 ms 141904 KB Output is correct
31 Correct 186 ms 186200 KB Output is correct
32 Correct 189 ms 186400 KB Output is correct
33 Correct 57 ms 141904 KB Output is correct
34 Correct 63 ms 142724 KB Output is correct
35 Correct 189 ms 186096 KB Output is correct
36 Correct 200 ms 186056 KB Output is correct
37 Correct 194 ms 186056 KB Output is correct
38 Correct 52 ms 141912 KB Output is correct
39 Correct 80 ms 142012 KB Output is correct
40 Correct 52 ms 141908 KB Output is correct
41 Correct 54 ms 141904 KB Output is correct
42 Correct 54 ms 141908 KB Output is correct
43 Correct 54 ms 141908 KB Output is correct
44 Correct 55 ms 141908 KB Output is correct
45 Correct 55 ms 141856 KB Output is correct
46 Correct 96 ms 160080 KB Output is correct
47 Correct 61 ms 141956 KB Output is correct
48 Correct 55 ms 142252 KB Output is correct
49 Correct 94 ms 160592 KB Output is correct
50 Correct 50 ms 141904 KB Output is correct
51 Correct 51 ms 141912 KB Output is correct
52 Correct 55 ms 141812 KB Output is correct
53 Correct 58 ms 142428 KB Output is correct
54 Correct 53 ms 142424 KB Output is correct
55 Correct 63 ms 142676 KB Output is correct
56 Correct 156 ms 173148 KB Output is correct
57 Correct 218 ms 165016 KB Output is correct
58 Correct 117 ms 158444 KB Output is correct
59 Correct 149 ms 184148 KB Output is correct
60 Correct 136 ms 190472 KB Output is correct
61 Correct 119 ms 155476 KB Output is correct
62 Correct 117 ms 153232 KB Output is correct
63 Correct 54 ms 142164 KB Output is correct
64 Correct 58 ms 142416 KB Output is correct
65 Correct 56 ms 142416 KB Output is correct
66 Correct 66 ms 142672 KB Output is correct
67 Correct 61 ms 145232 KB Output is correct
68 Correct 131 ms 173648 KB Output is correct
69 Correct 126 ms 166740 KB Output is correct
70 Correct 127 ms 159680 KB Output is correct
71 Correct 161 ms 185428 KB Output is correct
72 Correct 58 ms 141832 KB Output is correct
73 Correct 58 ms 142004 KB Output is correct
74 Correct 56 ms 141908 KB Output is correct
75 Correct 57 ms 141840 KB Output is correct
76 Correct 47 ms 141904 KB Output is correct
77 Correct 49 ms 141908 KB Output is correct
78 Correct 57 ms 142012 KB Output is correct
79 Correct 55 ms 141904 KB Output is correct
80 Correct 55 ms 141920 KB Output is correct
81 Correct 54 ms 142016 KB Output is correct
82 Correct 54 ms 141904 KB Output is correct
83 Correct 56 ms 142160 KB Output is correct
84 Correct 46 ms 141904 KB Output is correct
85 Correct 51 ms 142208 KB Output is correct
86 Correct 52 ms 141904 KB Output is correct
87 Correct 65 ms 142200 KB Output is correct
88 Correct 65 ms 141928 KB Output is correct
89 Correct 57 ms 143000 KB Output is correct
90 Correct 55 ms 142044 KB Output is correct
91 Correct 59 ms 142932 KB Output is correct
92 Correct 59 ms 141908 KB Output is correct
93 Correct 69 ms 141908 KB Output is correct
94 Correct 58 ms 141904 KB Output is correct
95 Correct 61 ms 142728 KB Output is correct
96 Correct 58 ms 141908 KB Output is correct
97 Correct 57 ms 141816 KB Output is correct
98 Correct 53 ms 142648 KB Output is correct
99 Correct 61 ms 142416 KB Output is correct
100 Correct 63 ms 142860 KB Output is correct
101 Correct 56 ms 142164 KB Output is correct
102 Correct 64 ms 142416 KB Output is correct
103 Correct 58 ms 142672 KB Output is correct
104 Correct 62 ms 142672 KB Output is correct
105 Correct 58 ms 143184 KB Output is correct
106 Correct 57 ms 142164 KB Output is correct
107 Correct 61 ms 142724 KB Output is correct
108 Correct 61 ms 142420 KB Output is correct
109 Correct 66 ms 143032 KB Output is correct
110 Correct 63 ms 142928 KB Output is correct
111 Correct 58 ms 142676 KB Output is correct
112 Correct 63 ms 142760 KB Output is correct
113 Correct 55 ms 142000 KB Output is correct
114 Correct 55 ms 141908 KB Output is correct
115 Correct 56 ms 141908 KB Output is correct
116 Correct 57 ms 141832 KB Output is correct
117 Correct 54 ms 141904 KB Output is correct
118 Correct 50 ms 142008 KB Output is correct
119 Correct 52 ms 141908 KB Output is correct
120 Correct 56 ms 141904 KB Output is correct
121 Correct 58 ms 141888 KB Output is correct
122 Correct 51 ms 142160 KB Output is correct
123 Correct 58 ms 141800 KB Output is correct
124 Correct 59 ms 142368 KB Output is correct
125 Correct 97 ms 161076 KB Output is correct
126 Correct 51 ms 141916 KB Output is correct
127 Correct 65 ms 142228 KB Output is correct
128 Correct 117 ms 159948 KB Output is correct
129 Correct 60 ms 142012 KB Output is correct
130 Correct 60 ms 142228 KB Output is correct
131 Correct 94 ms 161796 KB Output is correct
132 Correct 58 ms 141904 KB Output is correct
133 Correct 59 ms 142932 KB Output is correct
134 Correct 144 ms 197956 KB Output is correct
135 Correct 58 ms 141852 KB Output is correct
136 Correct 62 ms 142872 KB Output is correct
137 Correct 193 ms 190928 KB Output is correct
138 Correct 127 ms 248028 KB Output is correct
139 Correct 55 ms 141904 KB Output is correct
140 Correct 61 ms 141904 KB Output is correct
141 Correct 198 ms 187108 KB Output is correct
142 Correct 216 ms 187168 KB Output is correct
143 Correct 59 ms 142008 KB Output is correct
144 Correct 69 ms 143184 KB Output is correct
145 Correct 193 ms 187080 KB Output is correct
146 Correct 186 ms 186856 KB Output is correct
147 Correct 203 ms 187044 KB Output is correct
148 Correct 57 ms 141904 KB Output is correct
149 Correct 52 ms 141912 KB Output is correct
150 Correct 75 ms 142560 KB Output is correct
151 Correct 60 ms 142420 KB Output is correct
152 Correct 66 ms 142676 KB Output is correct
153 Correct 136 ms 174340 KB Output is correct
154 Correct 126 ms 165968 KB Output is correct
155 Correct 124 ms 158428 KB Output is correct
156 Correct 145 ms 183948 KB Output is correct
157 Correct 137 ms 190480 KB Output is correct
158 Correct 117 ms 155472 KB Output is correct
159 Correct 111 ms 153240 KB Output is correct
160 Correct 56 ms 142160 KB Output is correct
161 Correct 56 ms 142416 KB Output is correct
162 Correct 56 ms 142584 KB Output is correct
163 Correct 64 ms 142676 KB Output is correct
164 Correct 67 ms 145244 KB Output is correct
165 Correct 130 ms 173652 KB Output is correct
166 Correct 130 ms 166676 KB Output is correct
167 Correct 117 ms 159700 KB Output is correct
168 Correct 136 ms 185424 KB Output is correct
169 Correct 58 ms 143184 KB Output is correct
170 Correct 60 ms 142200 KB Output is correct
171 Correct 64 ms 142672 KB Output is correct
172 Correct 60 ms 142328 KB Output is correct
173 Correct 63 ms 142676 KB Output is correct
174 Correct 65 ms 142928 KB Output is correct
175 Correct 59 ms 142672 KB Output is correct
176 Correct 66 ms 142812 KB Output is correct
177 Correct 188 ms 206280 KB Output is correct
178 Correct 2056 ms 165204 KB Output is correct
179 Correct 145 ms 189388 KB Output is correct
180 Correct 68 ms 146516 KB Output is correct
181 Correct 186 ms 186916 KB Output is correct
182 Correct 68 ms 146512 KB Output is correct
183 Correct 64 ms 147428 KB Output is correct
184 Correct 65 ms 146516 KB Output is correct
185 Correct 71 ms 147348 KB Output is correct
186 Correct 237 ms 187456 KB Output is correct
187 Correct 219 ms 196944 KB Output is correct
188 Correct 236 ms 187404 KB Output is correct
189 Correct 227 ms 196948 KB Output is correct
190 Correct 231 ms 196160 KB Output is correct
191 Correct 201 ms 196948 KB Output is correct
192 Correct 74 ms 145488 KB Output is correct
193 Correct 205 ms 179112 KB Output is correct
194 Correct 100 ms 170128 KB Output is correct
195 Correct 58 ms 141848 KB Output is correct
196 Correct 54 ms 141904 KB Output is correct
197 Correct 58 ms 141904 KB Output is correct
198 Correct 58 ms 142008 KB Output is correct