#include <bits/stdc++.h>
#include "september.h"
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, P>;
const ll n_ = 1e5 + 10, inf = (ll)2e9 * (ll)1e9 + 7, mod = 998244353;
ll n, m, tc = 1, a, b, c, d, sum, x, y, z, base, ans, k;
ll par[n_], idx[n_][6], mn[n_], R[n_];
ll find(ll x) {
if (par[x] < 0) return x;
return par[x] = find(par[x]);
}
void merge(ll x, ll y) {
x = find(x), y = find(y);
if (x == y) return;
par[x] = y;
}
bool cmp(ll x, ll y) { return idx[x][a] < idx[y][a]; }
vector<ll> v[n_];
void dfs(ll x, ll i,ll val,ll node) {
if (idx[x][i] >= val) {
R[val] = max(R[val],idx[x][i]);
val = idx[x][i];
node = x;
}
for (auto nxt : v[x]) {
dfs(nxt, i, val, node);
}
}
int solve(int N, int M, vector<int> F, vector<vector<int>> S) {
n = N - 1, m = M;
for(int i=0;i<=n;i++)par[i]=-1;
vector<ll> T;
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++) idx[S[j][i]][j] = i;
for (int i = 1; i <= n; i++) T.push_back(i);
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++) {
a = i, b = j;
sort(all(T), cmp);
stack<ll> st;
for (auto nxt : T) {
while (st.size() && idx[st.top()][b] > idx[nxt][b]) {
merge(st.top(), nxt);
st.pop();
}
st.push(nxt);
}
}
for (int i = 1; i <= n; i++) {
v[F[i]].push_back(i);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j <= n; j++)R[j] = j;
for (auto nxt : v[0])dfs(nxt, i, idx[nxt][i], nxt);
for (int j = 0; j < n; j++) {
if (R[j] > j)merge(S[i][j], S[i][j + 1]);
R[j + 1] = max(R[j], R[j + 1]);
}
}
for(int i=0;i<=n;i++)v[i].clear();
ll ret = 0;
for (int i = 1; i <= n; i++) {
if (find(i) == i) ret++;
}
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |