#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,N) for(ll i = 0; i < N; i++)
#define all(x) (x).begin(), (x).end()
// #define F first
// #define S second
#include "september.h"
int solve(int N, int M, vector<int> P, vector<vector<int>> S) {
vector<vector<int>> C(N);
for(int i = 1; i < N; i++) C[P[i]].push_back(i);
unordered_set<int> hL; // hanging leaves
map<int, pair<bool,int>> done; // leaf is seen by worker 0, # times leaf is seen
set<pair<int,int>> check; // checking if every leaf has been seen the same amnt of times (by all the workers)
int K = 0;
FOR(i, S[0].size()) {
int cur = S[0][i];
done[cur].first = true;
if (hL.find(cur) != hL.end()) hL.erase(hL.find(cur));
C[P[cur]].erase(find(all(C[P[cur]]), (cur)));
for(auto c: C[cur]) {
if (!done[c].first) {
hL.insert(c);
}
}
FOR(j, M) {
int leaf = S[j][i];
if (done[leaf].second == 0) {
check.insert({1, leaf});
done[leaf].second++;
}
else {
check.erase(check.find({done[leaf].second, leaf}));
check.insert({done[leaf].second+1, leaf});
done[leaf].second++;
}
}
if (hL.empty() && check.begin()->first == prev(check.end())->first) K++;
}
return K;
}
// signed main() {
// cin.tie(0); ios::sync_with_stdio(false);
// }
# | 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... |