#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
using ar2 = array<int,2>;
const int mxN = (int)1e5+10;
const int INF = (int)1e9;
int n, k;
vector<int> adj[mxN];
ar2 ans{0,-INF};
vector<int> col[mxN];
int a[mxN], dep[mxN], sub[mxN];
vector<int> S[mxN];
set<ar2> Dep[mxN];
int dfs_timer, st[mxN], en[mxN];
void dfs1(int s, int p){
sub[s] = 1; st[s]=++dfs_timer;
if(p!=-1) dep[s]=dep[p]+1;
for(auto u : adj[s]){
if(u==p) continue;
dfs1(u,s); sub[s]+=sub[u];
}
en[s] = dfs_timer;
}
bool upper(int a, int b){
return st[a]<=st[b] and en[a]>=en[b];
}
void dfs(int s, int p){
S[s].pb(s); Dep[s].insert({dep[s],1});
for(auto u : adj[s]){
if(u==p) continue;
dfs(u,s);
if(sz(S[s]) < sz(S[u])) S[s].swap(S[u]), Dep[s].swap(Dep[u]);
for(auto v : S[u]) S[s].pb(v);
for(auto [v,w] : Dep[u]){
auto itr = Dep[s].lower_bound({v,-1});
if(itr!=end(Dep[s]) and (*itr)[0]==v){
int cur = (*itr)[1];
Dep[s].erase(itr);
Dep[s].insert({v,cur+w});
}
else Dep[s].insert({v,w});
}
}
if(min(sub[s],sz(col[a[s]])) < ans[0]) return;
auto cur = ar2({0,0});
unordered_map<int,int> tot; tot.clear();
for(auto u : col[a[s]]){
if(upper(s,u)) cur[0]++,tot[dep[u]]++;
}
for(auto u : col[a[s]]){
if(upper(s,u)) continue;
auto itr = Dep[s].lower_bound({dep[u],-1});
if(itr!=end(Dep[s]) and (*itr)[0]==dep[u]){
if(tot[dep[u]] < (*itr)[1]){
cur[0]++, cur[1]--; tot[dep[u]]++;
}
}
}
ans = max(ans, cur);
}
int main(){
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> a[i]; col[a[i]].pb(i);
}
for(int i = 1; i < n; i++){
int x; cin >> x; adj[i].pb(x), adj[x].pb(i);
}
dfs1(0,-1);
for(int i = 0; i < n; i++)
sort(all(adj[i]),[&](int a, int b){return sub[a]>sub[b]; });
dfs(0,-1); cout << ans[0] << " " << -ans[1] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12008 KB |
Output is correct |
3 |
Correct |
5 ms |
12212 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12132 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
12 ms |
12892 KB |
Output is correct |
9 |
Correct |
97 ms |
53448 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
74 ms |
12972 KB |
Output is correct |
12 |
Execution timed out |
4075 ms |
46768 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
77 ms |
12936 KB |
Output is correct |
5 |
Execution timed out |
4011 ms |
46824 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12376 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
7 ms |
12124 KB |
Output is correct |
8 |
Correct |
98 ms |
53164 KB |
Output is correct |
9 |
Correct |
6 ms |
12376 KB |
Output is correct |
10 |
Correct |
6 ms |
12892 KB |
Output is correct |
11 |
Correct |
98 ms |
53536 KB |
Output is correct |
12 |
Correct |
5 ms |
12124 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
4 ms |
12124 KB |
Output is correct |
15 |
Correct |
9 ms |
12376 KB |
Output is correct |
16 |
Correct |
7 ms |
12380 KB |
Output is correct |
17 |
Correct |
6 ms |
12380 KB |
Output is correct |
18 |
Correct |
133 ms |
32520 KB |
Output is correct |
19 |
Correct |
135 ms |
32716 KB |
Output is correct |
20 |
Correct |
201 ms |
36296 KB |
Output is correct |
21 |
Correct |
162 ms |
29332 KB |
Output is correct |
22 |
Correct |
111 ms |
30296 KB |
Output is correct |
23 |
Correct |
199 ms |
43212 KB |
Output is correct |
24 |
Correct |
213 ms |
39368 KB |
Output is correct |
25 |
Correct |
7 ms |
12636 KB |
Output is correct |
26 |
Correct |
6 ms |
12636 KB |
Output is correct |
27 |
Correct |
6 ms |
12380 KB |
Output is correct |
28 |
Correct |
6 ms |
12380 KB |
Output is correct |
29 |
Correct |
12 ms |
14172 KB |
Output is correct |
30 |
Correct |
124 ms |
32028 KB |
Output is correct |
31 |
Correct |
159 ms |
33364 KB |
Output is correct |
32 |
Correct |
161 ms |
36812 KB |
Output is correct |
33 |
Correct |
126 ms |
30364 KB |
Output is correct |
34 |
Correct |
7 ms |
12196 KB |
Output is correct |
35 |
Correct |
5 ms |
12380 KB |
Output is correct |
36 |
Correct |
6 ms |
12124 KB |
Output is correct |
37 |
Correct |
5 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12196 KB |
Output is correct |
4 |
Correct |
6 ms |
12216 KB |
Output is correct |
5 |
Correct |
4 ms |
12124 KB |
Output is correct |
6 |
Correct |
6 ms |
12124 KB |
Output is correct |
7 |
Correct |
6 ms |
12124 KB |
Output is correct |
8 |
Correct |
11 ms |
12972 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
75 ms |
13000 KB |
Output is correct |
11 |
Correct |
5 ms |
12120 KB |
Output is correct |
12 |
Correct |
7 ms |
12892 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
24 ms |
12448 KB |
Output is correct |
15 |
Correct |
6 ms |
12192 KB |
Output is correct |
16 |
Correct |
40 ms |
12696 KB |
Output is correct |
17 |
Correct |
4 ms |
12120 KB |
Output is correct |
18 |
Correct |
5 ms |
12124 KB |
Output is correct |
19 |
Correct |
7 ms |
12192 KB |
Output is correct |
20 |
Correct |
10 ms |
12376 KB |
Output is correct |
21 |
Correct |
6 ms |
12124 KB |
Output is correct |
22 |
Correct |
7 ms |
12124 KB |
Output is correct |
23 |
Correct |
6 ms |
12408 KB |
Output is correct |
24 |
Correct |
9 ms |
12380 KB |
Output is correct |
25 |
Correct |
7 ms |
12380 KB |
Output is correct |
26 |
Correct |
7 ms |
12636 KB |
Output is correct |
27 |
Correct |
7 ms |
12724 KB |
Output is correct |
28 |
Correct |
6 ms |
12380 KB |
Output is correct |
29 |
Correct |
9 ms |
12380 KB |
Output is correct |
30 |
Correct |
7 ms |
12760 KB |
Output is correct |
31 |
Correct |
7 ms |
12572 KB |
Output is correct |
32 |
Correct |
7 ms |
12416 KB |
Output is correct |
33 |
Correct |
6 ms |
12636 KB |
Output is correct |
34 |
Correct |
6 ms |
12468 KB |
Output is correct |
35 |
Correct |
7 ms |
12348 KB |
Output is correct |
36 |
Correct |
6 ms |
12380 KB |
Output is correct |
37 |
Correct |
6 ms |
12380 KB |
Output is correct |
38 |
Correct |
5 ms |
12204 KB |
Output is correct |
39 |
Correct |
6 ms |
12132 KB |
Output is correct |
40 |
Correct |
6 ms |
12016 KB |
Output is correct |
41 |
Correct |
6 ms |
12372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12008 KB |
Output is correct |
3 |
Correct |
5 ms |
12212 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12132 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
12 ms |
12892 KB |
Output is correct |
9 |
Correct |
97 ms |
53448 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
74 ms |
12972 KB |
Output is correct |
12 |
Execution timed out |
4075 ms |
46768 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |