|
|
发表于 2022-10-27 23:52:51
|
显示全部楼层
本楼为最佳答案
稍微改一改
- #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;
- }
复制代码 |
|