#include<bits/stdc++.h>
#include "beechtree.h"
using namespace std;
const int N = 200'000 + 10;
vector<int> child[N], subtree[N];
int n, h[N];
bool compare(int x, int y) {
if(child[x].size() == child[y].size()) return h[x] < h[y];
return child[x].size() > child[y].size();
}
bool check(int a, int b, vector<int> &C)
{
set<int> st;
for(int c : child[a])
st.insert(C[c]);
for(int c : child[b])
if(st.find(C[c]) == st.end())
return false;
return true;
}
void dfs(int v, vector<int> &ans, vector<int> &C, vector<int> &P)
{
subtree[v].push_back(v);
if(child[v].empty())
{
ans[v] = 1;
return;
}
for(int c : child[v])
{
h[c] = h[v] + 1;
dfs(c, ans, C, P);
for(int i : subtree[c])
subtree[v].push_back(i);
subtree[c].clear();
}
int cnt = 0;
set<int> st;
for(int c : child[v])
{
st.insert(C[c]);
if(!child[c].empty())
cnt++;
if(ans[c] == 0)
{
ans[v] = 0;
return;
}
}
if(st.size() != child[v].size())
{
ans[v] = 0;
return;
}
sort(subtree[v].begin(), subtree[v].end(), compare);
bool ok = (subtree[v][0] == v);
map<int,int> mp;
for(int i = 1; i < subtree[v].size() && ok; i++)
{
int u = subtree[v][i];
ok &= (subtree[v][mp[C[u]]] == P[u]);
mp[C[u]]++;
}
ans[v] = ok;
}
vector<int> beechtree(int N, int M, vector<int> P, vector<int> C)
{
for(int i = 1; i < N; i ++)
child[P[i]].push_back(i);
vector<int> ans(N);
dfs(0, ans, C, P);
return ans;
}
Compilation message
beechtree.cpp: In function 'void dfs(int, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
beechtree.cpp:69:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i = 1; i < subtree[v].size() && ok; i++)
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
3 ms |
10328 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10420 KB |
Output is correct |
16 |
Correct |
3 ms |
10328 KB |
Output is correct |
17 |
Correct |
4 ms |
10332 KB |
Output is correct |
18 |
Correct |
3 ms |
10332 KB |
Output is correct |
19 |
Correct |
3 ms |
10416 KB |
Output is correct |
20 |
Correct |
2 ms |
10332 KB |
Output is correct |
21 |
Correct |
2 ms |
10332 KB |
Output is correct |
22 |
Correct |
2 ms |
10332 KB |
Output is correct |
23 |
Correct |
2 ms |
10332 KB |
Output is correct |
24 |
Correct |
4 ms |
10332 KB |
Output is correct |
25 |
Correct |
2 ms |
10332 KB |
Output is correct |
26 |
Correct |
3 ms |
10328 KB |
Output is correct |
27 |
Correct |
3 ms |
10332 KB |
Output is correct |
28 |
Incorrect |
3 ms |
10436 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Runtime error |
1625 ms |
2097152 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10328 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Incorrect |
3 ms |
10332 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10328 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Runtime error |
1625 ms |
2097152 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10328 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
3 ms |
10328 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10420 KB |
Output is correct |
19 |
Correct |
3 ms |
10328 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10332 KB |
Output is correct |
22 |
Correct |
3 ms |
10416 KB |
Output is correct |
23 |
Correct |
2 ms |
10332 KB |
Output is correct |
24 |
Correct |
2 ms |
10332 KB |
Output is correct |
25 |
Correct |
2 ms |
10332 KB |
Output is correct |
26 |
Correct |
2 ms |
10332 KB |
Output is correct |
27 |
Correct |
4 ms |
10332 KB |
Output is correct |
28 |
Correct |
2 ms |
10332 KB |
Output is correct |
29 |
Correct |
3 ms |
10328 KB |
Output is correct |
30 |
Correct |
3 ms |
10332 KB |
Output is correct |
31 |
Incorrect |
3 ms |
10436 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10328 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10420 KB |
Output is correct |
12 |
Correct |
3 ms |
10328 KB |
Output is correct |
13 |
Correct |
4 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
15 |
Correct |
3 ms |
10416 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
19 |
Correct |
2 ms |
10332 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
2 ms |
10332 KB |
Output is correct |
22 |
Correct |
3 ms |
10328 KB |
Output is correct |
23 |
Correct |
3 ms |
10332 KB |
Output is correct |
24 |
Incorrect |
3 ms |
10436 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10328 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
3 ms |
10328 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10420 KB |
Output is correct |
19 |
Correct |
3 ms |
10328 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10332 KB |
Output is correct |
22 |
Correct |
3 ms |
10416 KB |
Output is correct |
23 |
Correct |
2 ms |
10332 KB |
Output is correct |
24 |
Correct |
2 ms |
10332 KB |
Output is correct |
25 |
Correct |
2 ms |
10332 KB |
Output is correct |
26 |
Correct |
2 ms |
10332 KB |
Output is correct |
27 |
Correct |
4 ms |
10332 KB |
Output is correct |
28 |
Correct |
2 ms |
10332 KB |
Output is correct |
29 |
Correct |
3 ms |
10328 KB |
Output is correct |
30 |
Correct |
3 ms |
10332 KB |
Output is correct |
31 |
Incorrect |
3 ms |
10436 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10328 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10420 KB |
Output is correct |
12 |
Correct |
3 ms |
10328 KB |
Output is correct |
13 |
Correct |
4 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
15 |
Correct |
3 ms |
10416 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
19 |
Correct |
2 ms |
10332 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
2 ms |
10332 KB |
Output is correct |
22 |
Correct |
3 ms |
10328 KB |
Output is correct |
23 |
Correct |
3 ms |
10332 KB |
Output is correct |
24 |
Incorrect |
3 ms |
10436 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10328 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
3 ms |
10328 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10420 KB |
Output is correct |
19 |
Correct |
3 ms |
10328 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10332 KB |
Output is correct |
22 |
Correct |
3 ms |
10416 KB |
Output is correct |
23 |
Correct |
2 ms |
10332 KB |
Output is correct |
24 |
Correct |
2 ms |
10332 KB |
Output is correct |
25 |
Correct |
2 ms |
10332 KB |
Output is correct |
26 |
Correct |
2 ms |
10332 KB |
Output is correct |
27 |
Correct |
4 ms |
10332 KB |
Output is correct |
28 |
Correct |
2 ms |
10332 KB |
Output is correct |
29 |
Correct |
3 ms |
10328 KB |
Output is correct |
30 |
Correct |
3 ms |
10332 KB |
Output is correct |
31 |
Incorrect |
3 ms |
10436 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
32 |
Halted |
0 ms |
0 KB |
- |