|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 zhangjinxuan 于 2022-11-4 21:35 编辑
描述和格式见后面的图片
输入样例:
- 7
- 2 7
- 3 6
- 4 5
- 0 0
- 0 0
- 0 0
- 0 0
复制代码
输出样例:
我的代码:
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- int n, a[1000001], d[1000001], x, y, ans = -(1 << 30);
- int main() {
- scanf("%d", &n);
- d[1] = 1;
- for (int i = 1; i <= n; ++i) {
- scanf("%d%d", &x, &y);
- if (x) ans = max(d[x] = d[i] + 1, ans);
- if (y) ans = max(d[y] = d[i] + 1, ans);
- }
- printf("%d", ans);
- }
复制代码
稍微改一改
- #include <iostream>
- #include <vector>
- #include <array>
- #include <algorithm>
- using std::vector, std::array;
- using std::cin, std::cout, std::endl;
- using std::istream;
- using std::max;
- using tree_t = vector<array<size_t, 2>>;
- istream &operator>>(istream &is, tree_t &tree) {
- tree_t temp; temp.push_back({}); // 不使用tree[0]
- size_t count; is >> count;
- for(size_t i = 0; i < count; ++i) {
- array<size_t, 2> a; is >> a[0] >> a[1];
- temp.push_back(a);
- }
- return tree = temp, is;
- }
- size_t get_depth(const tree_t &tree, size_t node, size_t init) {
- if(!node) return init;
- return ++init, max(get_depth(tree, tree[node][0], init), get_depth(tree, tree[node][1], init));
- }
- int main() {
- tree_t tree; cin >> tree;
- cout << get_depth(tree, 1, 0) << endl;
- return 0;
- }
复制代码
|
-
|