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;
const int N = 2e5 + 10;
int n , col[N] , m , sbt[N] , is_path[N] , cnt[N];
vector <int> adj[N];
vector <int> all , p;
void Dfs(int v)
{
sbt[v] = 1;
for(auto u : adj[v])
{
Dfs(u);
sbt[v] += sbt[u];
}
if(adj[v].size() == 1)
{
int c = adj[v][0];
if(sbt[c] == 1)
is_path[v] = col[c];
else if(col[c] == is_path[c])
is_path[v] = col[c];
else
is_path[v] = -1;
}
else if(adj[v].size() > 1)
{
is_path[v] = -1;
}
}
void Dfs_all(int v)
{
all.push_back(v);
cnt[col[v]] = 0;
for(auto u : adj[v])
Dfs_all(u);
}
bool Solve(int v)
{
all.clear();
Dfs_all(v);
sort(all.begin() , all.end());
bool flg = false;
do{
for(auto u : all)
cnt[col[u]] = 0;
bool tmp = true;
for(int i = 1 ; i < all.size() ; i++)
{
if(all[cnt[col[all[i]]]] != p[all[i]])
tmp = false;
cnt[col[all[i]]]++;
}
flg |= tmp;
}while(next_permutation(all.begin() , all.end()));
return flg;
}
vector<int> beechtree(int nn, int mm, vector<int> P, vector<int> C)
{
n = nn;
m = mm;
p = P;
for(int i = 1 ; i < n ; i++)
{
adj[P[i]].push_back(i);
col[i] = C[i];
}
Dfs(0);
vector <int> res(n , 0);
for(int i = 0 ; i < n ; i++)
{
if(is_path[i] != -1)
{
res[i] = true;
continue;
}
res[i] = Solve(i);
}
return res;
}
Compilation message (stderr)
beechtree.cpp: In function 'bool Solve(int)':
beechtree.cpp:53:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 1 ; i < all.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... |