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>
#include "beechtree.h"
using namespace std;
using pii = pair<int, int>;
using piii = tuple<int, int, int>;
vector<int> T[202020], C;
priority_queue<piii> Q;
int S[202020], I[202020];
set<pii> P1[202020];
set<piii> P2[202020];
int n, m;
bool dfs(int u, vector<int> &A) {
bool f = 1;
P1[u].emplace(I[u], u);
for (int &v: T[u]) {
auto it = P2[u].emplace(C[v], I[v], u).first;
if (it != P2[u].begin() && get<0>(*prev(it)) == C[v]) f = 0;
if (next(it) != P2[u].end() && get<0>(*next(it)) == C[v]) f = 0;
}
for (int &v: T[u]) {
f &= dfs(v, A);
if (P1[u].size() < P1[v].size()) {
swap(P1[u], P1[v]); swap(P2[u], P2[v]);
}
for (auto [i, w]: P1[v]) {
auto it1 = P1[u].emplace(i, w).first;
int c = 0;
for (int &x: T[w]) {
auto it2 = P2[u].emplace(C[x], I[x], w).first;
if (next(it2) != P2[u].end() && get<0>(*next(it2)) == C[x]) {
if (next(it1) == P1[u].end() || next(it1)->second != get<2>(*next(it2))) f = 0;
c++;
}
if (it1 != P1[u].begin()) {
if (it2 == P2[u].begin() || get<0>(*prev(it2)) != C[x] \
|| get<2>(*prev(it2)) != prev(it1)->second) f = 0;
}
}
if (next(it1) != P1[u].end() && c != T[next(it1)->second].size()) f = 0;
}
}
return A[u] = f;
}
vector<int> beechtree(int n, int m, vector<int> P, vector<int> C) {
::C = C; ::n = n; ::m = m;
int i;
for (i = 1; i < n; i++) {
T[P[i]].push_back(i);
}
fill(S, S + n, 1);
for (i = n - 1; i >= 1; i--) {
S[P[i]] += S[i];
}
Q.emplace(S[0], 0, 0);
for (i = 0; !Q.empty(); i++) {
auto [_, j, v] = Q.top();
I[v] = i; Q.pop();
for (int &w: T[v]) {
Q.emplace(S[w], -i, w);
}
}
vector<int> A(n);
dfs(0, A);
return A;
}
Compilation message (stderr)
beechtree.cpp: In function 'bool dfs(int, std::vector<int>&)':
beechtree.cpp:45:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if (next(it1) != P1[u].end() && c != T[next(it1)->second].size()) f = 0;
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
beechtree.cpp:49:14: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
49 | return A[u] = f;
# | 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... |