网易 魔法王国编程题,AC60%,求大佬看看哪里有问题
#include <iostream>
#include <vector>
using namespace std;
#define VISITED 2
int n;
int L;
int maze[50][50];
int maxnum;
void dfs(int i, int j, int length)
{
L--;
bool temp = false;
for (int m = 0; m < n; m++)
{
if (maze[j][m]==2)
{
temp = true;
break;
}
}
if (!temp)
{
length++;
}
maze[j][i]=maze[i][j] = VISITED;
if (L == 0) {
if (length > maxnum) {
maxnum = length;
}
return;
}
if (L > 0) {
for (int nx = 0; nx < n; ++nx) {
if (maze[j][nx] != 0)
{
int temp = maze[j][nx];
dfs(j, nx, length);
maze[j][nx]=maze[nx][j] = temp; // 注意:之前maze[x][y]被标记为VISITED(值为2),回退后应该将其还原为1
L++;
}
}
}
return;
}
int main()
{
cin >> n >> L;
int temp;
for (int i = 0; i<n; i++)
{
for (int j = 0; j<n; j++)
{
maze[i][j] = 0;
}
}
for (int i = 0; i <= n - 2; i++)
{
cin >> temp;
maze[i + 1][temp] = 1;
maze[temp][i + 1] = 1;
}
maxnum = 1;
for (int i = 0; i < n; i++)
{
if (maze[0][i] == 1)
{
dfs(0, i, 1);
}
}
cout << maxnum;
return 0;
}