# 2450 Problem B 树的高度

[献花][花圈][TK题库]

5
1 2
1 3
3 4
3 5

3

``````#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;
const int MaxN = 1000010;
typedef struct Tnode
{
vector<int>child;
bool NeedClear;
int level;
}TNode;

TNode Tree[MaxN];

int Deepth(int root)
{
int dp = 0;
queue <int> que;
que.push(root);

while(que.size())
{
int node = que.front();
que.pop();

for(int i=0;i<Tree[node].child.size();++i)
{
int childid = Tree[node].child[i];
Tree[childid].level = Tree[node].level + 1;
Tree[childid].NeedClear = true;
que.push(childid);

if(Tree[childid].level > dp)
dp = Tree[childid].level;
}
}

return dp;
}

int main()
{
#ifdef _Debug
freopen("data.txt","r+",stdin);
#endif

std::ios::sync_with_stdio(false);

int n,root,child;

while(cin >> n)
{
for(int i=0;i<n-1;++i)
{
cin >> root >> child;
if(Tree[root].NeedClear)
Tree[root].child.clear();
Tree[root].child.push_back(child);
}
Tree[1].level = 1;
cout << Deepth(1)<<endl;
}

return 0;
}

/************************************************************** Problem: 2450 User: Sharwen Language: C++ Result: 升仙 Time:177 ms Memory:32960 kb ****************************************************************/``````
原文作者：B树
原文地址: https://blog.csdn.net/fantasydreams/article/details/79339646
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。