This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
#include <bits/stdc++.h>
#include "beechtree.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define endl '\n'
#define sep ' '
#define pb push_back
#define Mp make_pair
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
const int maxn = 2e5 + 4;
int n, m;
int par[maxn], col[maxn];
vector<pii> adj[maxn], lsx[maxn];
int sz[maxn], val[maxn];
vector<int> ls[maxn], ans;
int arr[maxn]; set<pii> st[maxn];
bool cmp(int i, int j) {
return (lsx[i] < lsx[j]);
}
void calx(int v) {
for (auto f : adj[v]) {
int c = f.F, u = f.S;
lsx[v].pb(Mp(c, val[u]));
}
}
int GIx(int v, int c) {
int j = lower_bound(all(adj[v]), Mp(c, -1)) - adj[v].begin();
if (j < len(adj[v]) && adj[v][j].F == c) return j;
else return -1;
}
bool ok(int v1, int v2) {
if (val[v1] == val[v2]) return 1;
else if (val[v1] > val[v2] || len(adj[v1]) > len(adj[v2])) return 0;
for (auto f : adj[v1]) {
int c = f.F, u1 = f.S;
int j = GIx(v2, c);
if (j == -1) return 0;
int u2 = adj[v2][j].S;
if (val[u1] > val[u2]) return 0;
}
return 1;
}
bool addx(int v, int x) {
st[v].insert(Mp(val[x], x));
auto it = st[v].find(Mp(val[x], x));
if (it != st[v].begin()) {
auto itx = it; itx--;
if (!ok((*itx).S, (*it).S)) return 0;
}
auto itx = it; itx++;
if (itx != st[v].end()) {
if (!ok((*it).S, (*itx).S)) return 0;
}
return 1;
}
void pre_dfs(int v) {
sz[v] = 1;
for (auto f : adj[v]) {
int c = f.F, u = f.S;
pre_dfs(u);
sz[v] += sz[u];
}
}
void res_dfs(int v) {
for (auto f : adj[v]) {
int c = f.F, u = f.S;
res_dfs(u);
}
if (ans[v] == 0) return ;
for (auto f : adj[v]) {
int c = f.F, u = f.S;
if (ans[u] == 0) {
ans[v] = 0; return ;
}
}
addx(v, v);
for (auto f : adj[v]) {
int c = f.F, u = f.S;
if (len(st[u]) > len(st[v])) st[v].swap(st[u]);
for (auto g : st[u]) {
if (!addx(v, g.S)) {
ans[v] = 0; return ;
}
}
st[u].clear();
}
}
vector<int> beechtree(int Nx, int Mx, vector<int> Px, vector<int> Cx) {
n = Nx; m = Mx;
for (int i = 0; i < n; i++) Cx[i]--;
for (int i = 0; i < n; i++) {
par[i] = Px[i]; col[i] = Cx[i];
}
for (int i = 1; i < n; i++) {
adj[par[i]].pb(Mp(col[i], i));
}
for (int i = 0; i < n; i++) sort(all(adj[i]));
pre_dfs(0);
for (int v = 0; v < n; v++) ls[sz[v]].pb(v);
int x = 0;
for (int R = 1; R <= n; R++) {
int j = 0;
for (int v : ls[R]) {
calx(v);
arr[j++] = v;
}
sort(arr, arr + j, cmp);
for (int i = 0; i < j; i++) {
if (i - 1 >= 0) {
int u1 = arr[i - 1], u2 = arr[i];
if (lsx[u1] != lsx[u2]) x++;
}
int v = arr[i];
val[v] = x;
}
x++;
}
ans.resize(n);
for (int v = 0; v < n; v++) {
ans[v] = 1;
for (int j = 1; j < len(adj[v]); j++) {
if (adj[v][j - 1].F == adj[v][j].F) {
ans[v] = 0;
break;
}
}
}
res_dfs(0);
return ans;
}
Compilation message (stderr)
beechtree.cpp: In function 'void pre_dfs(int)':
beechtree.cpp:76:7: warning: unused variable 'c' [-Wunused-variable]
76 | int c = f.F, u = f.S;
| ^
beechtree.cpp: In function 'void res_dfs(int)':
beechtree.cpp:84:7: warning: unused variable 'c' [-Wunused-variable]
84 | int c = f.F, u = f.S;
| ^
beechtree.cpp:90:7: warning: unused variable 'c' [-Wunused-variable]
90 | int c = f.F, u = f.S;
| ^
beechtree.cpp:98:7: warning: unused variable 'c' [-Wunused-variable]
98 | int c = f.F, u = f.S;
| ^
# | 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... |