https://codeforces.com/gym/101933/problem/K
复杂度n的k次幂,暴力写法!
#include<bits/stdc++.h>
using namespace std;
int n,k;
const int N = 3e3+9;
vector<int> to[N];
int color[N];// 第i个节点是什么颜色!
int ans;
// dfs选择颜色每个数字的,然后dfs check;
bool check(int u,int fa)
{
if(fa != -1 && color[u] == color[fa]) return false;
for(auto v : to[u])
{
if(fa != v)
{
if(check(v,u) == false) return false; //这里必须得全部check后才能返回true
}
}
return true;
}
void dfs(int u)
{
if(u == n)
{
if(check(0,-1))
{
set<int> st;
for(int i=0; i<n; i++) st.insert(color[i]);
if(st.size() == k)
{
ans++;
}
}
return;
}
for(int i=1; i<=k; i++)
{
color[u] = i;
dfs(u+1);
}
}
int main()
{
cin >> n >> k;
for(int i=1; i<n; i++)
{
int fa;
cin >> fa;
to[fa].push_back(i);
}
dfs(0);
cout<< ans <<"\n";
return 0;
}