#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define iter(id, v) for(auto id : v)
#define fs first
#define se second
#define MP make_pair
#define pb push_back
#define bit(msk, i) ((msk >> i) & 1)
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 350;
int n, m;
int P[N];
vector<int> ke[N];
int pos[N];
pii rr[N];
int mnP[N], mxC[N];
void dfs (int u, int p) {
rr[u].fs = min(rr[u].fs, pos[u]);
mxC[u] = pos[u];
iter (&v, ke[u]) {
dfs(v, u);
mxC[u] = max(mxC[u], mxC[v]);
}
rr[u].se = max(rr[u].se, mxC[u]);
}
int solve (int _n, int _m, vector<int> _P, vector<vector<int>> _S) {
rep (i, 0, n) vector<int> ().swap(ke[i]);
n = _n;
m = _m;
--n;
rep (i, 1, n) {
P[i] = _P[i];
ke[P[i]].pb(i);
}
rep (i, 1, n) rr[i].fs = INF, rr[i].se = 0;
rep (j, 1, m) {
rep (i, 1, n) {
int x;
x = _S[j - 1][i - 1];
pos[x] = i;
}
dfs(0, 0);
mnP[0] = INF;
rep (i, 1, n) {
mnP[i] = min(mnP[P[i]], pos[i]);
if (mnP[i] < pos[i]) {
rr[i].fs = min(rr[i].fs, rr[P[i]].fs);
rr[i].se = max(rr[i].se, rr[P[i]].se);
}
}
}
sort (rr + 1, rr + 1 + n, [] (pii A, pii B) { return A.fs < B.fs || (A.fs == B.fs && A.se > B.se); });
int res = 0;
int L = -1, R = -1;
rep (i, 1, n) {
pii id = rr[i];
if (R < id.fs) {
++res;
tie(L, R) = id;
}
R = max(R, id.se);
}
return res;
}
void solution() {
rep (i, 0, n) vector<int> ().swap(ke[i]);
cin >> n >> m;
--n;
rep (i, 1, n) {
cin >> P[i];
ke[P[i]].pb(i);
}
rep (i, 1, n) rr[i].fs = INF, rr[i].se = 0;
rep (j, 1, m) {
rep (i, 1, n) {
int x; cin >> x;
pos[x] = i;
}
dfs(0, 0);
mnP[0] = INF;
rep (i, 1, n) {
mnP[i] = min(mnP[P[i]], pos[i]);
if (mnP[i] < pos[i]) {
rr[i].fs = min(rr[i].fs, rr[P[i]].fs);
rr[i].se = max(rr[i].se, rr[P[i]].se);
}
}
}
// rep (i, 1, n) cout << i<<": "<<rr[i].fs <<","<<rr[i].se <<"\n";
// return;
sort (rr + 1, rr + 1 + n, [] (pii A, pii B) { return A.fs < B.fs || (A.fs == B.fs && A.se > B.se); });
int res = 0;
int L = -1, R = -1;
rep (i, 1, n) {
pii id = rr[i];
if (R < id.fs) {
++res;
tie(L, R) = id;
}
R = max(R, id.se);
}
cout << res <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
//int main () {
//// file("c");
// ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int num_Test = 1;
// cout << solve(5, 2, {-1, 0, 0, 1, 1}, {{1, 2, 3, 4}, {4, 1, 2, 3}}) <<"\n";
//// cin >> num_Test;
//// while (num_Test--)
//// solution();
//}
/*
no bug +7
2
5 2
0 0 1 1
1 2 3 4
4 1 2 3
3 1
0 0
1 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2820 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 |
2820 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2908 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
3 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 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 |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2908 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
3 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2820 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 |
2820 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
3 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 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 |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2648 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2904 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2908 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
4 |
Correct |
125 ms |
23224 KB |
Output is correct |
5 |
Correct |
129 ms |
23460 KB |
Output is correct |
6 |
Correct |
121 ms |
23344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
3 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 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 |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
125 ms |
23224 KB |
Output is correct |
19 |
Correct |
129 ms |
23460 KB |
Output is correct |
20 |
Correct |
121 ms |
23344 KB |
Output is correct |
21 |
Correct |
171 ms |
15980 KB |
Output is correct |
22 |
Correct |
168 ms |
16040 KB |
Output is correct |
23 |
Correct |
155 ms |
16488 KB |
Output is correct |
24 |
Correct |
154 ms |
16672 KB |
Output is correct |
25 |
Correct |
181 ms |
15884 KB |
Output is correct |
26 |
Correct |
180 ms |
16124 KB |
Output is correct |
27 |
Correct |
163 ms |
17320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2908 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
3 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
125 ms |
23224 KB |
Output is correct |
8 |
Correct |
129 ms |
23460 KB |
Output is correct |
9 |
Correct |
121 ms |
23344 KB |
Output is correct |
10 |
Correct |
70 ms |
23496 KB |
Output is correct |
11 |
Correct |
68 ms |
23496 KB |
Output is correct |
12 |
Correct |
74 ms |
23496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2820 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 |
2820 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
3 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 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 |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2648 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2904 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
125 ms |
23224 KB |
Output is correct |
36 |
Correct |
129 ms |
23460 KB |
Output is correct |
37 |
Correct |
121 ms |
23344 KB |
Output is correct |
38 |
Correct |
171 ms |
15980 KB |
Output is correct |
39 |
Correct |
168 ms |
16040 KB |
Output is correct |
40 |
Correct |
155 ms |
16488 KB |
Output is correct |
41 |
Correct |
154 ms |
16672 KB |
Output is correct |
42 |
Correct |
181 ms |
15884 KB |
Output is correct |
43 |
Correct |
180 ms |
16124 KB |
Output is correct |
44 |
Correct |
163 ms |
17320 KB |
Output is correct |
45 |
Correct |
70 ms |
23496 KB |
Output is correct |
46 |
Correct |
68 ms |
23496 KB |
Output is correct |
47 |
Correct |
74 ms |
23496 KB |
Output is correct |
48 |
Correct |
83 ms |
15544 KB |
Output is correct |
49 |
Correct |
84 ms |
15536 KB |
Output is correct |
50 |
Correct |
74 ms |
16844 KB |
Output is correct |
51 |
Correct |
75 ms |
16632 KB |
Output is correct |
52 |
Correct |
84 ms |
15564 KB |
Output is correct |
53 |
Correct |
85 ms |
15544 KB |
Output is correct |
54 |
Correct |
81 ms |
16840 KB |
Output is correct |