本文主要是介绍Codeforces Beta Round #87 (Div. 2) / 116C Party (DFS树的最大深度),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://codeforces.com/problemset/problem/116/C
从树根DFS,看最大能递归几层。
/*30ms,300KB*/#include<bits/stdc++.h>
using namespace std;
const int mx = 2005;vector<int> v[mx];
bool fa[mx];
int maxlen;void dfs(int i, int deep)
{maxlen = max(maxlen, deep);for (int j = 0; j < v[i].size(); ++j)dfs(v[i][j], deep + 1);
}int main()
{memset(fa, 1, sizeof(fa));int n, x;cin >> n;for (int i = 1; i <= n; ++i){cin >> x;if (x != -1){v[x].push_back(i);fa[i] = false;}}for (int i = 1; i <= n; ++i)if (fa[i]) dfs(i, 1);printf("%d\n", maxlen);return 0;
}
这篇关于Codeforces Beta Round #87 (Div. 2) / 116C Party (DFS树的最大深度)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!