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;
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 (stderr)
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++)
| ~~^~~~~~~~~~~~~~~~~~~
# | 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... |