#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define el '\n'
#define ff first
#define ss second
#define pii pair <ll, ll>
#define pb push_back
#define mkp make_pair
#define fr(i, l, r) for(ll i = l; i <= r; i++)
#define debug(x) \
{ cout << #x << " = " << x << el; }
template<class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template<class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
const ll N = 2e5 + 10;
const ll M = 1e5 + 10;
const ll K = 400;
const ll INF = 1e18 + 10;
const ll inf = 1e9 + 10;
const ll LOG = 20;
const ll mod = 1000002022;
mt19937 rnd(time(0));
int p[N], c[N], cnt[N], ans[N], sz[N], h[N];
vector <int> g[N], x;
void dfs1(int v) {
x.pb(v);
for(int u : g[v])
dfs1(u);
}
bool comp(int a, int b) {
return sz[a] > sz[b];
}
int check(vector <int> &p) {
int k = 1;
for(int j = 1; j < x.size(); j++) {
int par = cnt[c[x[j]]];
cnt[c[x[j]]]++;
if(p[x[j]] != x[par]) k = 0;
}
for(int j = 0; j < x.size(); j++)
cnt[c[x[j]]] = 0;
return k;
}
void dfs2(int v) {
h[v] = 0;
sz[v] = 1;
int mx = 0, good = 1;
for(int u : g[v]) {
cnt[c[u]]++;
chmax(mx, cnt[c[u]]);
}
for(int u : g[v])
cnt[c[u]] = 0;
for(int u : g[v]) {
dfs2(u);
chmax(h[v], h[u] + 1);
sz[v] += sz[u];
good &= ans[u];
}
if(mx > 1 || h[v] > 2 || !good) return;
if(h[v] <= 1) {
ans[v] = 1;
return;
}
sort(g[v].begin(), g[v].end(), comp);
vector <int> p = {v};
for(int i : g[v])
p.pb(i);
for(int i : g[v])
for(int i2 : g[i])
p.pb(i2);
if(check(p)) ans[v] = 1;
}
std::vector<int> beechtree(int N, int M, std::vector<int> P, std::vector<int> C) {
fr(i, 0, N - 1) {
p[i] = P[i];
c[i] = C[i];
if(i) g[p[i]].pb(i);
}
bool k = 1;
fr(i, 0, N - 1)
k &= (p[i] == i - 1);
if(k) {
int cnt = 0;
for(int i = N - 1; i >= 0; i--) {
if(i + cnt == N - 1) ans[i] = 1;
cnt += (C[i] == C[N - 1]);
}
vector <int> res;
fr(i, 0, N - 1)
res.pb(ans[i]);
return res;
}
if(N <= 8) {
vector <int> ans(N);
fr(i, 0, N - 1) {
x.clear();
dfs1(i);
sort(x.begin(), x.end());
int good = 0;
do {
int k = 1;
if(x[0] != i)
continue;
for(int j = 1; j < x.size(); j++) {
int par = cnt[c[x[j]]];
cnt[c[x[j]]]++;
if(p[x[j]] != x[par]) k = 0;
}
for(int j = 0; j < x.size(); j++)
cnt[c[x[j]]] = 0;
if(k) {
good = 1;
break;
}
}while(next_permutation(x.begin(), x.end()));
ans[i] = good;
}
return ans;
}
dfs2(0);
vector <int> res;
fr(i, 0, N - 1)
res.pb(ans[i]);
return res;
}
/*
int main()
{
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> P(N);
for (int i = 0; i < N; ++i)
assert(1 == scanf("%d", &P[i]));
std::vector<int> C(N);
for (int i = 0; i < N; ++i)
assert(1 == scanf("%d", &C[i]));
fclose(stdin);
std::vector<int> res = beechtree(N, M, P, C);
int n = res.size();
for (int i = 0; i < n; ++i)
{
if (i > 0)
printf(" ");
printf("%d", res[i]);
}
printf("\n");
fclose(stdout);
return 0;
}
*/
Compilation message
beechtree.cpp: In function 'int check(std::vector<int>&)':
beechtree.cpp:50:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int j = 1; j < x.size(); j++) {
| ~~^~~~~~~~~~
beechtree.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int j = 0; j < x.size(); j++)
| ~~^~~~~~~~~~
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:134:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for(int j = 1; j < x.size(); j++) {
| ~~^~~~~~~~~~
beechtree.cpp:139:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for(int j = 0; j < x.size(); j++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8796 KB |
2nd lines differ - on the 4th token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8792 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 ms |
8796 KB |
Output is correct |
22 |
Correct |
1 ms |
8796 KB |
Output is correct |
23 |
Correct |
1 ms |
8792 KB |
Output is correct |
24 |
Correct |
1 ms |
8804 KB |
Output is correct |
25 |
Correct |
1 ms |
8804 KB |
Output is correct |
26 |
Correct |
1 ms |
8796 KB |
Output is correct |
27 |
Correct |
1 ms |
8796 KB |
Output is correct |
28 |
Correct |
1 ms |
8796 KB |
Output is correct |
29 |
Correct |
1 ms |
8792 KB |
Output is correct |
30 |
Correct |
1 ms |
8796 KB |
Output is correct |
31 |
Correct |
1 ms |
8796 KB |
Output is correct |
32 |
Correct |
1 ms |
8796 KB |
Output is correct |
33 |
Correct |
1 ms |
8796 KB |
Output is correct |
34 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
39 ms |
20232 KB |
Output is correct |
8 |
Correct |
39 ms |
21960 KB |
Output is correct |
9 |
Correct |
1 ms |
8792 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8816 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
38 ms |
21816 KB |
Output is correct |
18 |
Correct |
43 ms |
22220 KB |
Output is correct |
19 |
Correct |
39 ms |
22172 KB |
Output is correct |
20 |
Correct |
40 ms |
21964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
1 ms |
8796 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
39 ms |
20232 KB |
Output is correct |
6 |
Correct |
39 ms |
21960 KB |
Output is correct |
7 |
Incorrect |
1 ms |
8796 KB |
2nd lines differ - on the 32nd token, expected: '0', found: '1' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8796 KB |
2nd lines differ - on the 4th token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
1 ms |
8804 KB |
Output is correct |
21 |
Correct |
1 ms |
8804 KB |
Output is correct |
22 |
Correct |
1 ms |
8796 KB |
Output is correct |
23 |
Correct |
1 ms |
8796 KB |
Output is correct |
24 |
Correct |
1 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Incorrect |
1 ms |
8796 KB |
2nd lines differ - on the 6th token, expected: '1', found: '0' |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8796 KB |
2nd lines differ - on the 4th token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
1 ms |
8804 KB |
Output is correct |
21 |
Correct |
1 ms |
8804 KB |
Output is correct |
22 |
Correct |
1 ms |
8796 KB |
Output is correct |
23 |
Correct |
1 ms |
8796 KB |
Output is correct |
24 |
Correct |
1 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Incorrect |
1 ms |
8796 KB |
2nd lines differ - on the 6th token, expected: '1', found: '0' |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8796 KB |
2nd lines differ - on the 4th token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |