水题求教
  • 板块灌水区
  • 楼主Kano_zyc
  • 当前回复6
  • 已保存回复6
  • 发布时间2023/11/14 19:07
  • 上次更新2023/11/14 20:41:17
查看原帖
水题求教
1076440
Kano_zyc楼主2023/11/14 19:07

rt,蒟蒻一只,代码求条,不知道DFS为什么没有输出(大佬勿喷 link.

code

#include<bits/stdc++.h>
using namespace std;
const int N=3000*2+5;
bool a[N][N];
int path[N];
int n;
int ans;
void DFS(int cur, int de)
{
    ans = max(ans, de);

    for (int i = 1; i <= n; i++) {
        if (a[cur][i]) {
            path[i] = max(path[i], de + 1); 
            DFS(i, de + 1);
        }
    }
}
int main()
{
    scanf("%d",&n);
    int father;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d",&father);
        a[i][father]=true;
        a[father][i]=true;
    }
    DFS(1, 0); // 从国王开始
    printf("%d\n",ans);
    for(int i=1;i<=n;i++)printf("%d ",path[i]);
    return 0;
}
2023/11/14 19:07
加载中...