XXX
#include <iostream> #include <string> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int n, k; int gra[1010][1010]; bool vis[1010]; int ans = 0; void dfs(int node, int oil, int cnt) { if(oil == 0) { if(cnt > ans) ans = cnt; return; } vis[node]=true; for(int i=0; i<n; i++) { if(node != i && gra[node][i]==1 && !vis[i]) { dfs(i, oil-1, cnt+1); oil--; vis[i]=false; } } } int main() { scanf("%d %d", &n, &k); memset(gra, 0, sizeof(gra)); for(int i=0; i<n-1; i++) { int cur; scanf("%d", &cur); gra[i+1][cur] = gra[cur][i+1] = 1; } memset(vis, false, sizeof(vis)); dfs(0, 3, 0); cout << ans << endl; return 0; }
原文:https://www.cnblogs.com/yspworld/p/13672377.html