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 "beechtree.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n, m;
vector<int> p, c;
vector<int> adj[202020], res;
int sz[202020];
int has[202020];
void solve(int v) {
sz[v] = 1;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
solve(u);
sz[v] += sz[u];
has[v] += has[u];
}
if (adj[v].size() == 0) has[v] = 0;
else has[v]++;
if (has[v] > 2) return;
priority_queue< pair<pii, int> > pq;
vector<int> vec;
pq.push({{sz[v], 0}, v});
while (!pq.empty()) {
int now = pq.top().second;
vec.push_back(now);
pq.pop();
for (int i = 0; i < adj[now].size(); i++) {
int u = adj[now][i];
pq.push({{sz[u], -vec.size()}, u});
}
}
map<int, int> cnt;
for (int i = 1; i < sz[v]; i++) {
int now = vec[i], par;
if (cnt.find(c[now]) == cnt.end()) {
par = vec[0];
cnt.insert({c[now], 1});
}
else {
par = vec[cnt[c[now]]];
cnt[c[now]]++;
}
if (par != p[now]) {
res[v] = 0;
return;
}
}
}
vector<int> beechtree(int N, int M, vector<int> P, vector<int> C) {
n = N; m = M; p = P; c = C;
for (int i = 1; i < n; i++) {
adj[p[i]].push_back(i);
}
for (int i = 0; i < n; i++) res.push_back(1);
solve(0);
return res;
}
Compilation message (stderr)
beechtree.cpp: In function 'void solve(int)':
beechtree.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i = 0; i < adj[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~
beechtree.cpp:32:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 0; i < adj[now].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
# | 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... |