#include <bits/stdc++.h>
using namespace std;
int solve(int N, int M, vector<int> F, vector<vector<int>> S) {
vector<vector<int>> adj(N);
for(int i = 1; i<N; i++){
adj[i].push_back(F[i]);
adj[F[i]].push_back(i);
}
vector<int>l(N, INT_MAX);
vector<int>r(N, -1);
for(int i = 0; i<M; i++){
for(int j = 0; j<N-1; j++){
l[S[i][j]] = min(l[S[i][j]], j);
r[S[i][j]] = max(r[S[i][j]], j);
}
}
auto dfs = [&](int v, int p, auto&& dfs) -> void{
for(int u: adj[v]){
if(u==p) continue;
dfs(u, v, dfs);
if(v != 0 && l[v] <= r[u]){
l[v] = min(l[v], l[u]);
r[v] = max(r[v], r[u]);
}
}
return;
};
dfs(0, -1, dfs);
vector<array<int, 2>> itv(N-1);
for(int i = 1; i<N; i++) itv[i-1] = {l[i], r[i]};
sort(itv.begin(), itv.end());
int sol = 1;
int curr_l = itv[0][0];
int curr_r = itv[0][1];
for(int i = 1; i<N-1; i++){
if(curr_l <= itv[i][0] && itv[i][0] <= curr_r){
curr_r = max(curr_r, itv[i][1]);
}
else{
sol++;
curr_l = itv[i][0]; curr_r = itv[i][1];
}
}
return sol;
}
# | 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... |