This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "september.h"
#include <bits/stdc++.h>
using namespace std;
int solve(int n, int m, vector<int> par, vector<vector<int>> s) {
vector<int> p(n);
iota(p.begin(), p.end(), 0);
vector<int> from(n, n), to(n, 0);
function<int(int)> GetPar = [&](int x) {
return p[x] == x ? x : p[x] = GetPar(p[x]);
};
auto Merge = [&](int x, int y) {
x = GetPar(x);
y = GetPar(y);
if (x == y) {
return;
}
from[x] = min(from[x], from[y]);
to[x] = max(to[x], to[y]);
p[y] = x;
};
for (int i = 0; i < m; i++) {
for (int j = 0; j + 1 < n; j++) {
from[s[i][j]] = min(from[s[i][j]], j);
to[s[i][j]] = max(to[s[i][j]], j);
}
}
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
g[par[i]].push_back(i);
}
int T = 0;
vector<int> dfn(n);
vector<int> dfo(n);
function<void(int)> Dfs = [&](int v) {
dfn[v] = ++T;
for (int u : g[v]) {
Dfs(u);
}
dfo[v] = ++T;
};
Dfs(0);
auto IsAnc = [&](int x, int y) {
return dfn[x] <= dfn[y] && dfo[y] <= dfo[x];
};
for (int k = 0; k < m; k++) {
for (int i = 0; i + 1 < n; i++) {
for (int j = i + 1; j + 1 < n; j++) {
if (IsAnc(s[k][i], s[k][j])) {
Merge(s[k][i], s[k][j]);
}
}
}
}
for (int i = 1; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (GetPar(i) != i || GetPar(j) != j) {
continue;
}
if (to[i] < from[j] || to[j] < from[i]) {
continue;
}
Merge(i, j);
}
}
int res = 0;
for (int i = 1; i < n; i++) {
res += (GetPar(i) == i);
}
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |