Submission #1045468

# Submission time Handle Problem Language Result Execution time Memory
1045468 2024-08-06T02:06:39 Z ainta Team Coding (EGOI24_teamcoding) C++17
100 / 100
996 ms 62424 KB
#include <bits/stdc++.h>
using namespace std;

#define rng(i,a,b) for(int i=int(a);i<=int(b);i++)
#define rep(i,b) rng(i,0,b-1)
#define gnr(i,b,a) for(int i=int(b);i>=int(a);i--)
#define per(i,b) gnr(i,b-1,0)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
typedef long long ll;
using pii=pair<int,int>;
using vi=vc<int>;
using uint=unsigned;
using ull=unsigned long long;
using pil=pair<int,ll>;
using pli=pair<ll,int>;
using pll=pair<ll,ll>;
using t3=tuple<int,int,int>;

#define N_ 110000

vi E[N_], G[N_];
int n, K;
const int BL = 200;
int w[N_], Dep[N_], Num[N_], cnt, Ed[N_], Rev[N_];
int Left[N_], Right[N_], Ans, Ans2, T1[N_], T2[N_];
map<int,int>C[N_];
void DFS(int a){
    Num[a] = ++cnt;
    for(auto &x:E[a]){
        Dep[x]=Dep[a]+1;
        DFS(x);
    }
    Ed[a] = ++cnt;
}
void Do1(int a){
    int pv = -1, Mx = -1;
    for(auto &x:E[a]){
        Do1(x);
        if(Mx < si(C[x])){
            pv = x;
            Mx = si(C[x]);
        }
    }
    if(pv!=-1){
        swap(C[a],C[pv]);
    }
    C[a][Dep[a]]+=1;
    for(auto &x:E[a]){
        if(x==pv)continue;
        for(auto &[d,c] : C[x]) C[a][d] += c;
    }
    if(si(G[w[a]]) < BL){
        for(auto &t: G[w[a]]){
            T1[Dep[t]]++;
            if(Num[a] <= Num[t] && Num[t] <= Ed[a]){
                T2[Dep[t]]++;
            }
        }
        int s = 0, s2 = 0;
        for(auto &t: G[w[a]]){
            int d = Dep[t];
            if(!T1[d])continue;
            int c = C[a][d];
            s += min(T1[d], c);
            s2 += min(T2[d],c);
            T1[d]=T2[d]=0;
        }
        if(Ans < s || (Ans==s && Ans2 > s-s2)){
            Ans = s;
            Ans2 = s-s2;
        }
    }
}
int DMX, CC[N_], s2;
void Go(int a, int col){
    int d = Dep[a];
    CC[d]++;
    DMX = max(DMX, d);
    if(w[a]==col)s2++;

    for(auto &x: E[a]){
        Go(x,col);
    }
}
void Do2(int a, int col){
    if(w[a]==col){
        DMX = Dep[a];
        s2=0;
        Go(a, col);
        int s=0;
        rng(i,Dep[a],DMX){
            s += min(CC[i],T1[i]);
            CC[i]=0;
        }
        if(Ans < s || (Ans==s && Ans2 > s-s2)){
            Ans = s;
            Ans2 = s-s2;
        }
        return;
    }
    for(auto &x : E[a]){
        Do2(x, col);
    }
}

int main(){
    scanf("%d%d",&n,&K);
    rep(i,n){
        scanf("%d",&w[i]);
        G[w[i]].pb(i);
    }
    rng(i,1,n-1){
        int a;
        scanf("%d",&a);
        E[a].pb(i);
    }
    DFS(0);
    Do1(0);

    rep(i,K){
        if(si(G[i]) >= BL){
            for(auto &u: G[i])T1[Dep[u]]++;
            Do2(0,i);
            for(auto &u: G[i])T1[Dep[u]]--;
        }
    }
    printf("%d %d\n",Ans,Ans2);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:116:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |     scanf("%d%d",&n,&K);
      |     ~~~~~^~~~~~~~~~~~~~
Main.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         scanf("%d",&w[i]);
      |         ~~~~~^~~~~~~~~~~~
Main.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |         scanf("%d",&a);
      |         ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12632 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12664 KB Output is correct
8 Correct 5 ms 13148 KB Output is correct
9 Correct 116 ms 35232 KB Output is correct
10 Correct 2 ms 12892 KB Output is correct
11 Correct 2 ms 13148 KB Output is correct
12 Correct 38 ms 34348 KB Output is correct
13 Correct 3 ms 12888 KB Output is correct
14 Correct 3 ms 13092 KB Output is correct
15 Correct 83 ms 35772 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 13128 KB Output is correct
5 Correct 41 ms 34440 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 29 ms 20180 KB Output is correct
9 Correct 2 ms 12892 KB Output is correct
10 Correct 2 ms 13148 KB Output is correct
11 Correct 52 ms 26444 KB Output is correct
12 Correct 17 ms 18776 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12892 KB Output is correct
15 Correct 54 ms 20084 KB Output is correct
16 Correct 49 ms 20160 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 3 ms 12892 KB Output is correct
19 Correct 41 ms 20080 KB Output is correct
20 Correct 65 ms 19916 KB Output is correct
21 Correct 34 ms 20172 KB Output is correct
22 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12876 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12892 KB Output is correct
8 Correct 92 ms 35268 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 93 ms 35868 KB Output is correct
12 Correct 3 ms 12632 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 4 ms 12888 KB Output is correct
16 Correct 5 ms 13144 KB Output is correct
17 Correct 2 ms 12892 KB Output is correct
18 Correct 96 ms 26196 KB Output is correct
19 Correct 115 ms 31428 KB Output is correct
20 Correct 337 ms 62424 KB Output is correct
21 Correct 103 ms 30784 KB Output is correct
22 Correct 67 ms 23636 KB Output is correct
23 Correct 107 ms 28904 KB Output is correct
24 Correct 88 ms 26356 KB Output is correct
25 Correct 3 ms 13148 KB Output is correct
26 Correct 3 ms 13196 KB Output is correct
27 Correct 3 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 6 ms 13916 KB Output is correct
30 Correct 80 ms 27288 KB Output is correct
31 Correct 76 ms 28136 KB Output is correct
32 Correct 125 ms 30716 KB Output is correct
33 Correct 61 ms 24192 KB Output is correct
34 Correct 2 ms 12636 KB Output is correct
35 Correct 2 ms 12636 KB Output is correct
36 Correct 2 ms 12636 KB Output is correct
37 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12836 KB Output is correct
5 Correct 2 ms 12632 KB Output is correct
6 Correct 3 ms 12632 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 5 ms 13148 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 2 ms 13148 KB Output is correct
11 Correct 2 ms 12892 KB Output is correct
12 Correct 3 ms 13148 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12892 KB Output is correct
15 Correct 3 ms 12888 KB Output is correct
16 Correct 2 ms 13144 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12892 KB Output is correct
19 Correct 2 ms 12892 KB Output is correct
20 Correct 2 ms 12892 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
22 Correct 2 ms 12636 KB Output is correct
23 Correct 4 ms 12892 KB Output is correct
24 Correct 4 ms 13148 KB Output is correct
25 Correct 2 ms 12892 KB Output is correct
26 Correct 2 ms 13108 KB Output is correct
27 Correct 3 ms 13148 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 2 ms 12892 KB Output is correct
30 Correct 2 ms 12988 KB Output is correct
31 Correct 5 ms 13148 KB Output is correct
32 Correct 2 ms 12892 KB Output is correct
33 Correct 3 ms 13144 KB Output is correct
34 Correct 3 ms 12892 KB Output is correct
35 Correct 3 ms 12892 KB Output is correct
36 Correct 3 ms 13148 KB Output is correct
37 Correct 3 ms 12892 KB Output is correct
38 Correct 2 ms 12636 KB Output is correct
39 Correct 2 ms 12636 KB Output is correct
40 Correct 2 ms 12636 KB Output is correct
41 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12632 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12664 KB Output is correct
8 Correct 5 ms 13148 KB Output is correct
9 Correct 116 ms 35232 KB Output is correct
10 Correct 2 ms 12892 KB Output is correct
11 Correct 2 ms 13148 KB Output is correct
12 Correct 38 ms 34348 KB Output is correct
13 Correct 3 ms 12888 KB Output is correct
14 Correct 3 ms 13092 KB Output is correct
15 Correct 83 ms 35772 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
19 Correct 2 ms 12892 KB Output is correct
20 Correct 2 ms 13128 KB Output is correct
21 Correct 41 ms 34440 KB Output is correct
22 Correct 2 ms 12632 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 29 ms 20180 KB Output is correct
25 Correct 2 ms 12892 KB Output is correct
26 Correct 2 ms 13148 KB Output is correct
27 Correct 52 ms 26444 KB Output is correct
28 Correct 17 ms 18776 KB Output is correct
29 Correct 2 ms 12636 KB Output is correct
30 Correct 2 ms 12892 KB Output is correct
31 Correct 54 ms 20084 KB Output is correct
32 Correct 49 ms 20160 KB Output is correct
33 Correct 3 ms 12892 KB Output is correct
34 Correct 3 ms 12892 KB Output is correct
35 Correct 41 ms 20080 KB Output is correct
36 Correct 65 ms 19916 KB Output is correct
37 Correct 34 ms 20172 KB Output is correct
38 Correct 2 ms 12636 KB Output is correct
39 Correct 3 ms 12636 KB Output is correct
40 Correct 2 ms 12636 KB Output is correct
41 Correct 2 ms 12636 KB Output is correct
42 Correct 2 ms 12876 KB Output is correct
43 Correct 2 ms 12636 KB Output is correct
44 Correct 2 ms 12636 KB Output is correct
45 Correct 2 ms 12892 KB Output is correct
46 Correct 92 ms 35268 KB Output is correct
47 Correct 3 ms 12892 KB Output is correct
48 Correct 4 ms 13148 KB Output is correct
49 Correct 93 ms 35868 KB Output is correct
50 Correct 3 ms 12632 KB Output is correct
51 Correct 2 ms 12636 KB Output is correct
52 Correct 2 ms 12636 KB Output is correct
53 Correct 4 ms 12888 KB Output is correct
54 Correct 5 ms 13144 KB Output is correct
55 Correct 2 ms 12892 KB Output is correct
56 Correct 96 ms 26196 KB Output is correct
57 Correct 115 ms 31428 KB Output is correct
58 Correct 337 ms 62424 KB Output is correct
59 Correct 103 ms 30784 KB Output is correct
60 Correct 67 ms 23636 KB Output is correct
61 Correct 107 ms 28904 KB Output is correct
62 Correct 88 ms 26356 KB Output is correct
63 Correct 3 ms 13148 KB Output is correct
64 Correct 3 ms 13196 KB Output is correct
65 Correct 3 ms 12892 KB Output is correct
66 Correct 3 ms 12892 KB Output is correct
67 Correct 6 ms 13916 KB Output is correct
68 Correct 80 ms 27288 KB Output is correct
69 Correct 76 ms 28136 KB Output is correct
70 Correct 125 ms 30716 KB Output is correct
71 Correct 61 ms 24192 KB Output is correct
72 Correct 2 ms 12636 KB Output is correct
73 Correct 2 ms 12636 KB Output is correct
74 Correct 2 ms 12636 KB Output is correct
75 Correct 2 ms 12636 KB Output is correct
76 Correct 2 ms 12636 KB Output is correct
77 Correct 2 ms 12636 KB Output is correct
78 Correct 2 ms 12632 KB Output is correct
79 Correct 2 ms 12836 KB Output is correct
80 Correct 2 ms 12632 KB Output is correct
81 Correct 3 ms 12632 KB Output is correct
82 Correct 3 ms 12892 KB Output is correct
83 Correct 5 ms 13148 KB Output is correct
84 Correct 2 ms 12636 KB Output is correct
85 Correct 2 ms 13148 KB Output is correct
86 Correct 2 ms 12892 KB Output is correct
87 Correct 3 ms 13148 KB Output is correct
88 Correct 2 ms 12636 KB Output is correct
89 Correct 2 ms 12892 KB Output is correct
90 Correct 3 ms 12888 KB Output is correct
91 Correct 2 ms 13144 KB Output is correct
92 Correct 2 ms 12636 KB Output is correct
93 Correct 2 ms 12892 KB Output is correct
94 Correct 2 ms 12892 KB Output is correct
95 Correct 2 ms 12892 KB Output is correct
96 Correct 2 ms 12636 KB Output is correct
97 Correct 2 ms 12636 KB Output is correct
98 Correct 4 ms 12892 KB Output is correct
99 Correct 4 ms 13148 KB Output is correct
100 Correct 2 ms 12892 KB Output is correct
101 Correct 2 ms 13108 KB Output is correct
102 Correct 3 ms 13148 KB Output is correct
103 Correct 3 ms 12892 KB Output is correct
104 Correct 2 ms 12892 KB Output is correct
105 Correct 2 ms 12988 KB Output is correct
106 Correct 5 ms 13148 KB Output is correct
107 Correct 2 ms 12892 KB Output is correct
108 Correct 3 ms 13144 KB Output is correct
109 Correct 3 ms 12892 KB Output is correct
110 Correct 3 ms 12892 KB Output is correct
111 Correct 3 ms 13148 KB Output is correct
112 Correct 3 ms 12892 KB Output is correct
113 Correct 2 ms 12636 KB Output is correct
114 Correct 2 ms 12636 KB Output is correct
115 Correct 2 ms 12636 KB Output is correct
116 Correct 2 ms 12636 KB Output is correct
117 Correct 2 ms 12636 KB Output is correct
118 Correct 2 ms 12636 KB Output is correct
119 Correct 2 ms 12636 KB Output is correct
120 Correct 2 ms 12636 KB Output is correct
121 Correct 2 ms 12636 KB Output is correct
122 Correct 2 ms 12636 KB Output is correct
123 Correct 2 ms 12892 KB Output is correct
124 Correct 5 ms 13148 KB Output is correct
125 Correct 87 ms 35212 KB Output is correct
126 Correct 2 ms 12888 KB Output is correct
127 Correct 3 ms 13148 KB Output is correct
128 Correct 42 ms 34240 KB Output is correct
129 Correct 2 ms 12892 KB Output is correct
130 Correct 3 ms 13148 KB Output is correct
131 Correct 73 ms 35692 KB Output is correct
132 Correct 2 ms 12636 KB Output is correct
133 Correct 2 ms 12892 KB Output is correct
134 Correct 26 ms 20180 KB Output is correct
135 Correct 3 ms 12892 KB Output is correct
136 Correct 2 ms 13148 KB Output is correct
137 Correct 40 ms 26332 KB Output is correct
138 Correct 16 ms 18776 KB Output is correct
139 Correct 2 ms 12728 KB Output is correct
140 Correct 2 ms 12892 KB Output is correct
141 Correct 36 ms 19928 KB Output is correct
142 Correct 32 ms 20016 KB Output is correct
143 Correct 2 ms 12888 KB Output is correct
144 Correct 2 ms 12892 KB Output is correct
145 Correct 37 ms 20028 KB Output is correct
146 Correct 40 ms 20052 KB Output is correct
147 Correct 54 ms 19964 KB Output is correct
148 Correct 2 ms 12636 KB Output is correct
149 Correct 2 ms 12636 KB Output is correct
150 Correct 2 ms 12892 KB Output is correct
151 Correct 3 ms 13148 KB Output is correct
152 Correct 2 ms 12892 KB Output is correct
153 Correct 62 ms 26448 KB Output is correct
154 Correct 92 ms 31304 KB Output is correct
155 Correct 278 ms 62292 KB Output is correct
156 Correct 93 ms 30804 KB Output is correct
157 Correct 58 ms 23856 KB Output is correct
158 Correct 85 ms 29012 KB Output is correct
159 Correct 95 ms 26244 KB Output is correct
160 Correct 2 ms 12888 KB Output is correct
161 Correct 3 ms 13148 KB Output is correct
162 Correct 3 ms 12892 KB Output is correct
163 Correct 2 ms 12892 KB Output is correct
164 Correct 6 ms 13916 KB Output is correct
165 Correct 68 ms 27248 KB Output is correct
166 Correct 108 ms 28244 KB Output is correct
167 Correct 99 ms 30648 KB Output is correct
168 Correct 56 ms 24176 KB Output is correct
169 Correct 2 ms 13148 KB Output is correct
170 Correct 6 ms 13148 KB Output is correct
171 Correct 2 ms 12892 KB Output is correct
172 Correct 3 ms 13144 KB Output is correct
173 Correct 3 ms 12892 KB Output is correct
174 Correct 2 ms 12892 KB Output is correct
175 Correct 4 ms 13148 KB Output is correct
176 Correct 2 ms 12892 KB Output is correct
177 Correct 73 ms 27856 KB Output is correct
178 Correct 996 ms 21952 KB Output is correct
179 Correct 53 ms 25680 KB Output is correct
180 Correct 4 ms 13400 KB Output is correct
181 Correct 39 ms 20196 KB Output is correct
182 Correct 5 ms 13404 KB Output is correct
183 Correct 5 ms 13404 KB Output is correct
184 Correct 4 ms 13404 KB Output is correct
185 Correct 4 ms 13400 KB Output is correct
186 Correct 43 ms 19840 KB Output is correct
187 Correct 30 ms 19168 KB Output is correct
188 Correct 44 ms 19740 KB Output is correct
189 Correct 35 ms 18900 KB Output is correct
190 Correct 41 ms 19064 KB Output is correct
191 Correct 31 ms 19072 KB Output is correct
192 Correct 5 ms 13404 KB Output is correct
193 Correct 43 ms 20148 KB Output is correct
194 Correct 50 ms 27216 KB Output is correct
195 Correct 2 ms 12632 KB Output is correct
196 Correct 1 ms 12636 KB Output is correct
197 Correct 2 ms 12728 KB Output is correct
198 Correct 2 ms 12636 KB Output is correct