답안 #1052731

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1052731 2024-08-10T20:02:37 Z Ludissey Team Coding (EGOI24_teamcoding) C++17
100 / 100
1150 ms 72028 KB
#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()

using namespace std;
vector<vector<int>> child;
vector<int> depthCOUNT;
vector<int> depth;
vector<vector<int>> parent;
int totMX=0;
int totSWITCH=0;
int SQRT;
vector<int> l;
int need;
const int LOG=31;

int lca(int a, int b){
    if(depth[a]>depth[b]) swap(a,b);
    int d=depth[b]-depth[a];
    for (int i = LOG-1; i >= 0; i--)
    {
        if(d&(1<<i)) b=parent[b][i];
    }
    if(a==b) return a;
    for (int i = LOG-1; i >= 0; i--){
        if(parent[a][i]!=parent[b][i]){
            a=parent[a][i];
            b=parent[b][i];
        }
    }
    return parent[a][0];
}
vector<int> depthHAS;
vector<int> depthNOTHAS;
vector<int> depthNEDED;
vector<vector<int>> lang;
int N;
int addALLCHILD(int x){
    if(l[x]==need) depthHAS[depth[x]]++;
    else depthNOTHAS[depth[x]]++;
    int sm=depth[x];
    for (auto u : child[x])
    {
        sm=max(sm,addALLCHILD(u));
    }
    return sm;
}
void dfs(int x){
    if(l[x]==need){
        int mx=addALLCHILD(x);        
        int sm=1;
        int swtch=0;
        for (int j = depth[x]+1; j <= mx;j++)
        {
            int gave=min(depthNEDED[j]-depthHAS[j],depthNOTHAS[j]);
            sm+=depthHAS[j]+gave;
            swtch+=gave;
        }
        for (int j = depth[x]; j <= mx;j++)
        {
            depthHAS[j]=0;
            depthNOTHAS[j]=0;
        }
        if(sm>totMX){
            totMX=sm;
            totSWITCH=swtch;
        }else if(sm==totMX){
            totSWITCH=min(totSWITCH,swtch);
        }
        return;
    }else{
        for (auto u : child[x])
        {
            dfs(u);
        }
        return;
    }
}

unordered_map<int,int> dfs2(int x){
    vector<unordered_map<int,int>> mps;
    for (auto u : child[x])
    {
        mps.push_back(dfs2(u));
    }
    mps.push_back({});
    mps[sz(mps)-1][depth[x]]++;
    sort(all(mps), [&](auto &aa, auto &bb) {return sz(aa)>sz(bb);});
    for (int i = 1; i < sz(mps); i++)
    {
        for (auto u : mps[i])
        {
            mps[0][u.first]+=u.second;
        }
    }
    unordered_map<int,pair<int,int>> dpth;
    if(sz(lang[l[x]])<SQRT){
        for (int i = 0; i < sz(lang[l[x]]); i++)
        {
            if(lang[l[x]][i]==x||depth[lang[l[x]][i]]<=depth[x]) continue;
            if(lca(lang[l[x]][i],x)==x){
                dpth[depth[lang[l[x]][i]]].first++;
            }else{
                dpth[depth[lang[l[x]][i]]].second++;
            }
        }
        int sm=1;
        int swtch=0;
        for (auto u : dpth)
        {
            int gave=min(u.second.second,mps[0][u.first]-u.second.first);
            sm+=u.second.first+gave;
            swtch+=gave;
        }
        if(sm>totMX){
            totMX=sm;
            totSWITCH=swtch;
        }else if(sm==totMX){
            totSWITCH=min(totSWITCH,swtch);
        }
    }
    return move(mps[0]);
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int K; cin >> N >> K;
    l.resize(N);
    parent.resize(N,vector<int>(LOG,0));
    depth.resize(N);    
    child.resize(N);  
    lang.resize(K);  
    depthHAS.resize(N,0);
    depthNOTHAS.resize(N,0);
    SQRT=sqrt(N);
    unordered_map<int,int> cnt;
    for (int i = 0; i < N; i++) cin >> l[i];
    for (int i = 0; i < N; i++) cnt[l[i]]++;
    for (int i = 0; i < N; i++) lang[l[i]].push_back(i);
    
    int mx=cnt[l[0]];
    need=1-l[0];
    parent[0][0]=0;
    for (int i = 1; i < N; i++) {
        cin >> parent[i][0];
        child[parent[i][0]].push_back(i);
    }
    for (int j = 1; j < LOG; j++)
    {
        for (int i = 0; i < N; i++)
        {
            parent[i][j]=parent[parent[i][j-1]][j-1];
        }
    }
    depthNEDED.resize(N,0);    
    queue<int> que;
    que.push(0);
    while(!que.empty()){
        int x=que.front(); que.pop();
        for (auto u : child[x])
        {
            depth[u]=depth[x]+1;
            que.push(u);
        }
    }
    dfs2(0);
    for (int i = 0; i < K; i++)
    {
        if(sz(lang[i])>=SQRT){
            for (int j = 0; j < sz(lang[i]); j++) depthNEDED[depth[lang[i][j]]]++;
            need=i;
            dfs(0);
            for (int j = 0; j < sz(lang[i]); j++) depthNEDED[depth[lang[i][j]]]=0;;

        }
    }
    
    if(mx>=totMX){
        totMX=mx;
        totSWITCH=0;
    }
    cout << totMX <<  " " << totSWITCH << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 456 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 1628 KB Output is correct
9 Correct 133 ms 70136 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 1628 KB Output is correct
12 Correct 79 ms 65400 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 2 ms 1884 KB Output is correct
15 Correct 116 ms 71832 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 1628 KB Output is correct
5 Correct 67 ms 65172 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 71 ms 49080 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 1372 KB Output is correct
11 Correct 81 ms 54392 KB Output is correct
12 Correct 62 ms 55744 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 71 ms 37500 KB Output is correct
16 Correct 69 ms 37364 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 66 ms 37404 KB Output is correct
20 Correct 67 ms 37580 KB Output is correct
21 Correct 70 ms 37576 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 460 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 129 ms 70268 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 2 ms 1628 KB Output is correct
11 Correct 122 ms 72028 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 1164 KB Output is correct
16 Correct 2 ms 1268 KB Output is correct
17 Correct 2 ms 1368 KB Output is correct
18 Correct 116 ms 43256 KB Output is correct
19 Correct 141 ms 42340 KB Output is correct
20 Correct 298 ms 50004 KB Output is correct
21 Correct 191 ms 42668 KB Output is correct
22 Correct 124 ms 47260 KB Output is correct
23 Correct 200 ms 58668 KB Output is correct
24 Correct 95 ms 53424 KB Output is correct
25 Correct 2 ms 1372 KB Output is correct
26 Correct 2 ms 1628 KB Output is correct
27 Correct 2 ms 1116 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
29 Correct 8 ms 4700 KB Output is correct
30 Correct 142 ms 41972 KB Output is correct
31 Correct 124 ms 44132 KB Output is correct
32 Correct 171 ms 50532 KB Output is correct
33 Correct 104 ms 45984 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
# 결과 실행 시간 메모리 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 1628 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 1628 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 1372 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 1372 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 428 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 1116 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 2 ms 1116 KB Output is correct
24 Correct 2 ms 1116 KB Output is correct
25 Correct 2 ms 1372 KB Output is correct
26 Correct 2 ms 1372 KB Output is correct
27 Correct 2 ms 1628 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
29 Correct 2 ms 1116 KB Output is correct
30 Correct 2 ms 1628 KB Output is correct
31 Correct 2 ms 1336 KB Output is correct
32 Correct 1 ms 1116 KB Output is correct
33 Correct 2 ms 1116 KB Output is correct
34 Correct 2 ms 1116 KB Output is correct
35 Correct 1 ms 1116 KB Output is correct
36 Correct 1 ms 1116 KB Output is correct
37 Correct 1 ms 1116 KB Output is correct
38 Correct 0 ms 348 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 456 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 1628 KB Output is correct
9 Correct 133 ms 70136 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 1628 KB Output is correct
12 Correct 79 ms 65400 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 2 ms 1884 KB Output is correct
15 Correct 116 ms 71832 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 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 1 ms 1628 KB Output is correct
21 Correct 67 ms 65172 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 1372 KB Output is correct
24 Correct 71 ms 49080 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 1372 KB Output is correct
27 Correct 81 ms 54392 KB Output is correct
28 Correct 62 ms 55744 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 71 ms 37500 KB Output is correct
32 Correct 69 ms 37364 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 1 ms 1116 KB Output is correct
35 Correct 66 ms 37404 KB Output is correct
36 Correct 67 ms 37580 KB Output is correct
37 Correct 70 ms 37576 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 460 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 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 129 ms 70268 KB Output is correct
47 Correct 1 ms 344 KB Output is correct
48 Correct 2 ms 1628 KB Output is correct
49 Correct 122 ms 72028 KB Output is correct
50 Correct 0 ms 344 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 1164 KB Output is correct
54 Correct 2 ms 1268 KB Output is correct
55 Correct 2 ms 1368 KB Output is correct
56 Correct 116 ms 43256 KB Output is correct
57 Correct 141 ms 42340 KB Output is correct
58 Correct 298 ms 50004 KB Output is correct
59 Correct 191 ms 42668 KB Output is correct
60 Correct 124 ms 47260 KB Output is correct
61 Correct 200 ms 58668 KB Output is correct
62 Correct 95 ms 53424 KB Output is correct
63 Correct 2 ms 1372 KB Output is correct
64 Correct 2 ms 1628 KB Output is correct
65 Correct 2 ms 1116 KB Output is correct
66 Correct 2 ms 1116 KB Output is correct
67 Correct 8 ms 4700 KB Output is correct
68 Correct 142 ms 41972 KB Output is correct
69 Correct 124 ms 44132 KB Output is correct
70 Correct 171 ms 50532 KB Output is correct
71 Correct 104 ms 45984 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 0 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 348 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 2 ms 1628 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 1 ms 1628 KB Output is correct
86 Correct 0 ms 348 KB Output is correct
87 Correct 2 ms 1628 KB Output is correct
88 Correct 0 ms 348 KB Output is correct
89 Correct 1 ms 1372 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 1 ms 1372 KB Output is correct
92 Correct 0 ms 348 KB Output is correct
93 Correct 0 ms 428 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 1 ms 1116 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 0 ms 344 KB Output is correct
98 Correct 2 ms 1116 KB Output is correct
99 Correct 2 ms 1116 KB Output is correct
100 Correct 2 ms 1372 KB Output is correct
101 Correct 2 ms 1372 KB Output is correct
102 Correct 2 ms 1628 KB Output is correct
103 Correct 2 ms 1116 KB Output is correct
104 Correct 2 ms 1116 KB Output is correct
105 Correct 2 ms 1628 KB Output is correct
106 Correct 2 ms 1336 KB Output is correct
107 Correct 1 ms 1116 KB Output is correct
108 Correct 2 ms 1116 KB Output is correct
109 Correct 2 ms 1116 KB Output is correct
110 Correct 1 ms 1116 KB Output is correct
111 Correct 1 ms 1116 KB Output is correct
112 Correct 1 ms 1116 KB Output is correct
113 Correct 0 ms 348 KB Output is correct
114 Correct 0 ms 348 KB Output is correct
115 Correct 0 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 0 ms 348 KB Output is correct
120 Correct 0 ms 348 KB Output is correct
121 Correct 0 ms 348 KB Output is correct
122 Correct 0 ms 348 KB Output is correct
123 Correct 0 ms 348 KB Output is correct
124 Correct 2 ms 1628 KB Output is correct
125 Correct 161 ms 70244 KB Output is correct
126 Correct 1 ms 348 KB Output is correct
127 Correct 2 ms 1588 KB Output is correct
128 Correct 65 ms 65324 KB Output is correct
129 Correct 0 ms 344 KB Output is correct
130 Correct 2 ms 1628 KB Output is correct
131 Correct 112 ms 71780 KB Output is correct
132 Correct 0 ms 348 KB Output is correct
133 Correct 1 ms 1372 KB Output is correct
134 Correct 78 ms 48516 KB Output is correct
135 Correct 0 ms 344 KB Output is correct
136 Correct 1 ms 1372 KB Output is correct
137 Correct 81 ms 54428 KB Output is correct
138 Correct 64 ms 56708 KB Output is correct
139 Correct 0 ms 348 KB Output is correct
140 Correct 0 ms 348 KB Output is correct
141 Correct 71 ms 37536 KB Output is correct
142 Correct 90 ms 37484 KB Output is correct
143 Correct 0 ms 344 KB Output is correct
144 Correct 1 ms 1116 KB Output is correct
145 Correct 69 ms 37476 KB Output is correct
146 Correct 69 ms 37572 KB Output is correct
147 Correct 66 ms 37328 KB Output is correct
148 Correct 0 ms 344 KB Output is correct
149 Correct 0 ms 348 KB Output is correct
150 Correct 2 ms 1400 KB Output is correct
151 Correct 2 ms 1116 KB Output is correct
152 Correct 2 ms 1372 KB Output is correct
153 Correct 118 ms 43148 KB Output is correct
154 Correct 143 ms 42164 KB Output is correct
155 Correct 273 ms 50000 KB Output is correct
156 Correct 168 ms 42672 KB Output is correct
157 Correct 108 ms 47524 KB Output is correct
158 Correct 162 ms 58676 KB Output is correct
159 Correct 116 ms 53352 KB Output is correct
160 Correct 2 ms 1368 KB Output is correct
161 Correct 2 ms 1628 KB Output is correct
162 Correct 2 ms 1116 KB Output is correct
163 Correct 1 ms 1116 KB Output is correct
164 Correct 8 ms 4700 KB Output is correct
165 Correct 124 ms 42052 KB Output is correct
166 Correct 147 ms 44128 KB Output is correct
167 Correct 135 ms 50648 KB Output is correct
168 Correct 147 ms 45992 KB Output is correct
169 Correct 2 ms 1624 KB Output is correct
170 Correct 3 ms 1116 KB Output is correct
171 Correct 1 ms 1116 KB Output is correct
172 Correct 2 ms 1116 KB Output is correct
173 Correct 2 ms 1116 KB Output is correct
174 Correct 1 ms 1116 KB Output is correct
175 Correct 1 ms 1112 KB Output is correct
176 Correct 2 ms 1112 KB Output is correct
177 Correct 118 ms 60256 KB Output is correct
178 Correct 1150 ms 43856 KB Output is correct
179 Correct 131 ms 44232 KB Output is correct
180 Correct 6 ms 4188 KB Output is correct
181 Correct 69 ms 39280 KB Output is correct
182 Correct 5 ms 4184 KB Output is correct
183 Correct 7 ms 4444 KB Output is correct
184 Correct 5 ms 4400 KB Output is correct
185 Correct 7 ms 4444 KB Output is correct
186 Correct 68 ms 39864 KB Output is correct
187 Correct 61 ms 37708 KB Output is correct
188 Correct 75 ms 39400 KB Output is correct
189 Correct 68 ms 37508 KB Output is correct
190 Correct 63 ms 38368 KB Output is correct
191 Correct 70 ms 42800 KB Output is correct
192 Correct 6 ms 4188 KB Output is correct
193 Correct 76 ms 38000 KB Output is correct
194 Correct 104 ms 45260 KB Output is correct
195 Correct 1 ms 344 KB Output is correct
196 Correct 0 ms 348 KB Output is correct
197 Correct 0 ms 348 KB Output is correct
198 Correct 0 ms 348 KB Output is correct