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;
for (int &x: T[w]) {
auto it2 = P2[u].emplace(C[x], I[x], x).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;
}
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;
}
}
}
}
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:46:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
46 | 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... |