# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
998113 | Trisanu_Das | September (APIO24_september) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MAX_M = 15;
class UnionFind {
private:
vector<int> parent, size;
public:
UnionFind(int n) : parent(n), size(n, 1) {
iota(parent.begin(), parent.end(), 0);
}
int find(int v) {
if (parent[v] == v) return v;
return parent[v] = find(parent[v]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (size[a] < size[b]) swap(a, b);
parent[b] = a;
size[a] += size[b];
}
}
int getSize(int v) {
return size[find(v)];
}
};
int solve(int n, int m, const vector<int>& F, const vector<vector<int>>& S) {
vector<vector<int>> graph(n);
vector<UnionFind> uf(m, UnionFind(n));
vector<set<int>> elements(m);
for (int i = 1; i < n; ++i) {
graph[i].push_back(F[i]);
graph[F[i]].push_back(i);
}
int answer = 0;
for (int i = n - 2; i >= 0; --i) {
bool isValid = true;
for (int j = 0; j < m; ++j) {
int x = S[j][i];
elements[j].insert(x);
for (int neighbor : graph[x]) {
if (elements[j].count(neighbor)) {
uf[j].unite(x, neighbor);
}
}
if (uf[j].getSize(x) != n - i) {
isValid = false;
}
}
if (n <= 1000) {
for (int j = 1; j < m; ++j) {
if (elements[j] != elements[j - 1]) {
isValid = false;
}
}
}
if (isValid) ++answer;
}
return answer;
}