#include "september.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct segTree{
int tree[400002];
void init(int i, int l, int r){
tree[i] = 0;
if(l==r) return;
int m = (l+r)>>1;
init(i*2, l, m);
init(i*2+1, m+1, r);
}
void update(int i, int l, int r, int x, int v){
if(l==r){
tree[i] = v;
return;
}
int m = (l+r)>>1;
if(x<=m) update(i*2, l, m, x, v);
else update(i*2+1, m+1, r, x, v);
tree[i] = max(tree[i*2], tree[i*2+1]);
}
int query(int i, int l, int r, int v){ /// [1, i] 최댓값이 v보다 큰 인 최소 i 반환
if(tree[i] <= v) return -1;
if(l==r) return l;
int m = (l+r)>>1;
if(tree[i*2] > v) return query(i*2, l, m, v);
else return query(i*2+1, m+1, r, v);
}
} tree;
struct UnionFind{
int par[100002];
void init(int n){
for(int i=1; i<=n; i++) par[i] = i;
}
int find(int x){
if(x == par[x]) return x;
return x = find(par[x]);
}
void merge(int x, int y){
x = find(x), y = find(y);
if(x>y) swap(x, y);
par[y] = x;
}
} dsu[5];
struct Query{
int a, b;
Query(int a, int b): a(a), b(b){}
};
int n, m;
int par[100002];
vector<int> link[100002];
int arr[5][100002], ord[5][100002];
int DP[100002];
void dfs(int x){
for(int y: link[x]){
if(x) DP[y] = min(DP[y], DP[x]);
dfs(y);
}
}
int solve(int N, int M, vector<int> F, vector<vector<int>> S) {
n = N, m = M;
for(int i=1; i<n; i++) par[i] = F[i], link[par[i]].push_back(i);
for(int i=0; i<m; i++) for(int j=1; j<N; j++){
arr[i][j] = S[i][j-1];
ord[i][arr[i][j]] = j;
}
vector<Query> queries;
/// Case 1
for(int d=0; d<m-1; d++){
tree.init(1, 1, n-1);
for(int i=1; i<n; i++){
int x = tree.query(1, 1, n-1, ord[d+1][arr[d][i]]);
if(x != -1) queries.push_back(Query(arr[d][x], arr[d][i]));
tree.update(1, 1, n-1, i, ord[d+1][arr[d][i]]);
}
}
/// Case 2
for(int d=0; d<m; d++){
for(int i=0; i<n; i++) DP[i] = ord[d][i];
dfs(0);
for(int i=0; i<n; i++){
if(DP[i] < ord[d][i]){
queries.push_back(Query(arr[d][DP[i]], i));
}
}
}
/// 본격 작업 시작
for(int d=0; d<m; d++) dsu[d].init(n-1);
while(!queries.empty()){
int A = queries.back().a, B = queries.back().b; queries.pop_back();
for(int d=0; d<m; d++){
int ad = dsu[d].find(ord[d][A]), bd = dsu[d].find(ord[d][B]);
if(ad > bd) swap(ad, bd);
while(ad != bd){
queries.push_back(Query(arr[d][bd-1], arr[d][bd]));
dsu[d].merge(bd-1, bd);
bd = dsu[d].find(bd);
}
}
}
int ans = 0;
for(int i=1; i<n; i++) if(dsu[0].find(i) == i) ans++;
/// 초기화
for(int i=0; i<=n; i++){
link[i].clear();
DP[i] = 0;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2716 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2812 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2716 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2812 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2712 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2904 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2716 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2812 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2904 KB |
Output is correct |
12 |
Correct |
2 ms |
2664 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2680 KB |
Output is correct |
16 |
Correct |
2 ms |
2912 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2904 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
4 ms |
3164 KB |
Output is correct |
5 |
Correct |
3 ms |
3164 KB |
Output is correct |
6 |
Correct |
3 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2716 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2812 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2712 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2904 KB |
Output is correct |
19 |
Correct |
2 ms |
2664 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2680 KB |
Output is correct |
23 |
Correct |
2 ms |
2912 KB |
Output is correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Correct |
4 ms |
3164 KB |
Output is correct |
26 |
Correct |
3 ms |
3164 KB |
Output is correct |
27 |
Correct |
3 ms |
3164 KB |
Output is correct |
28 |
Correct |
3 ms |
2908 KB |
Output is correct |
29 |
Correct |
4 ms |
3092 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
31 |
Correct |
5 ms |
3140 KB |
Output is correct |
32 |
Correct |
4 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
2908 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2904 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
109 ms |
21732 KB |
Output is correct |
5 |
Correct |
118 ms |
23116 KB |
Output is correct |
6 |
Correct |
105 ms |
21644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2716 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2812 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2904 KB |
Output is correct |
12 |
Correct |
2 ms |
2664 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2680 KB |
Output is correct |
16 |
Correct |
2 ms |
2912 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
109 ms |
21732 KB |
Output is correct |
19 |
Correct |
118 ms |
23116 KB |
Output is correct |
20 |
Correct |
105 ms |
21644 KB |
Output is correct |
21 |
Correct |
119 ms |
16804 KB |
Output is correct |
22 |
Correct |
116 ms |
16812 KB |
Output is correct |
23 |
Correct |
129 ms |
17912 KB |
Output is correct |
24 |
Correct |
115 ms |
17936 KB |
Output is correct |
25 |
Correct |
116 ms |
16728 KB |
Output is correct |
26 |
Correct |
118 ms |
16836 KB |
Output is correct |
27 |
Correct |
114 ms |
17832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2904 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
4 ms |
3164 KB |
Output is correct |
5 |
Correct |
3 ms |
3164 KB |
Output is correct |
6 |
Correct |
3 ms |
3164 KB |
Output is correct |
7 |
Correct |
109 ms |
21732 KB |
Output is correct |
8 |
Correct |
118 ms |
23116 KB |
Output is correct |
9 |
Correct |
105 ms |
21644 KB |
Output is correct |
10 |
Correct |
164 ms |
29444 KB |
Output is correct |
11 |
Correct |
172 ms |
28908 KB |
Output is correct |
12 |
Correct |
166 ms |
29680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2716 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2812 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2712 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2904 KB |
Output is correct |
19 |
Correct |
2 ms |
2664 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2680 KB |
Output is correct |
23 |
Correct |
2 ms |
2912 KB |
Output is correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Correct |
4 ms |
3164 KB |
Output is correct |
26 |
Correct |
3 ms |
3164 KB |
Output is correct |
27 |
Correct |
3 ms |
3164 KB |
Output is correct |
28 |
Correct |
3 ms |
2908 KB |
Output is correct |
29 |
Correct |
4 ms |
3092 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
31 |
Correct |
5 ms |
3140 KB |
Output is correct |
32 |
Correct |
4 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
2908 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
109 ms |
21732 KB |
Output is correct |
36 |
Correct |
118 ms |
23116 KB |
Output is correct |
37 |
Correct |
105 ms |
21644 KB |
Output is correct |
38 |
Correct |
119 ms |
16804 KB |
Output is correct |
39 |
Correct |
116 ms |
16812 KB |
Output is correct |
40 |
Correct |
129 ms |
17912 KB |
Output is correct |
41 |
Correct |
115 ms |
17936 KB |
Output is correct |
42 |
Correct |
116 ms |
16728 KB |
Output is correct |
43 |
Correct |
118 ms |
16836 KB |
Output is correct |
44 |
Correct |
114 ms |
17832 KB |
Output is correct |
45 |
Correct |
164 ms |
29444 KB |
Output is correct |
46 |
Correct |
172 ms |
28908 KB |
Output is correct |
47 |
Correct |
166 ms |
29680 KB |
Output is correct |
48 |
Correct |
324 ms |
22896 KB |
Output is correct |
49 |
Correct |
204 ms |
22468 KB |
Output is correct |
50 |
Correct |
306 ms |
24172 KB |
Output is correct |
51 |
Correct |
329 ms |
24412 KB |
Output is correct |
52 |
Correct |
145 ms |
22212 KB |
Output is correct |
53 |
Correct |
186 ms |
21948 KB |
Output is correct |
54 |
Correct |
165 ms |
23492 KB |
Output is correct |