原题如下:
小葱有一颗树,这棵树有n个节点,n−1
条边,点的编号是1,2,⋯,n。
小葱对树很熟悉了,树的深度、树的各种遍历,她都会求。
现在小葱想知道树的宽度是多少!
小葱对宽度的定义是:树的的节点数最多的那一层的节点数。
输入描述
第一行一个数字n。 随后n−1行,每行一对数字,代表一条边。 最后一行一个数字,代表根节点的编号。
输出描述
一行一个数字,代表树的宽度。
输入样例
5 5
1 2 8
1 3 1
1 5 3
2 4 5
3 4 2
输出样例
8
我的原来写的代码(一直崩溃而且还错):
#include<bits/stdc++.h>
using namespace std;
typedef struct Bitree {
int ch;
Bitree* chs[151];
};
int treeWidth(Bitree *root){
if(!root){
return 0;
}
int width = 0;
int maxWidth = 0;
queue<Bitree *> Q;
Bitree *p = 0;
Q.push(root);
while(!Q.empty()){
width = Q.size();
if(maxWidth < width){
maxWidth = width;
}
for(int i=0; i<width; i++){
p = Q.front();
Q.pop();
for (int i = 0; i < p->ch; i++)
Q.push(p->chs[i]);
}
}
return maxWidth;
}
int n;
Bitree tr[101];
int root,N;
int main() {
int n;
vector<int> e[N];
cin >>n;
for(int i=1;i< n; i++){
int x,y;
cin >>x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
cin >>root;
cout <<treeWidth(tr + root);
return 0;
}
能看看出什么问题了吗?