Login
Register
Problem list
Online status
Huhu_Miao
:
2024-12-31 22:43:08
/** * author: Huhu_Miao * created: 2024.12.31 22:41:00 (UTC+8) * 建树早已无法优雅AC,暴力仍能光宗耀祖! **/ #include
using pii = std::pair
; void solve(){ int n; std::cin >> n; if( n == 0){ std::cout << 0 << '\n'; return; } std::vector
vec(n+1); for(int i = 0 ; i < n ; i++){ int from , a , b; std::cin >> from >> a >> b; vec[from] = {a,b}; } std::vector
cnt(n+1 , 0); cnt[0] = -1; for(int i = 1; i <= n; i++){ int child = vec[i].first; while(child != -1){ cnt[i]++; child = vec[child].second; } } int max = -1; for(const auto elem: cnt){ if(elem > max) max = elem; } std::cout << max <<'\n'; } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int T; std::cin>>T; while(T--) solve(); return 0; }
徐鹏
:
2024-11-28 16:18:37
#include
#include
#include
#include
using namespace std; // 树的节点结构 struct TreeNode { int id; TreeNode* firstChild; TreeNode* nextSibling; TreeNode(int i) : id(i), firstChild(nullptr), nextSibling(nullptr) {} }; int dfs(TreeNode* node) { if (node == nullptr) { return 0; } // 递归计算孩子的高度 int childHeight = dfs(node->firstChild); return childHeight + 1; } int main() { int T; cin >> T; while (T--) { int n; cin >> n; // 创建节点数组并初始化 vector
nodes(n + 1); for (int i = 1; i <= n; ++i) { nodes[i] = new TreeNode(i); } // 读取孩子和兄弟信息并构建树 for (int i = 1; i <= n; ++i) { int id, son, bro; cin >> id >> son >> bro; if (son != -1) { nodes[id]->firstChild = nodes[son]; } } // 计算并输出树的高度 int height = dfs(nodes[1]); // 根节点ID为1 cout << height << endl; for (auto& node : nodes) { delete node; } } return 0; } 不明白为什么answer wrong
Post Your Comment