Codevs 15,012 fork Tree max width and height

Source: Internet
Author: User

Address: http://codevs.cn/problem/1501/


First contact with the tree.

Fortunately, the node of this problem is certain, not arbitrarily set. (In fact, the Internet to find the correct understanding of the wrong idea.) )

So do not think so much with deep search.

Otherwise I can't do it for the time being.


#include <iostream>

using namespace std;

int width[10], tree[20][2];
int Layernum, widthnum;


void Dfs (int i, int layer) {//i is the number of nodes 
	width[layer]++;//each more node, the width of the corresponding layer is plus 1 
	if (Layer > Layernum) layernum = Layer //To determine the maximum number of layers 
	if (tree[i][0]!= 0) DFS (tree[i][0], layer + 1);
	if (Tree[i][1]!= 0) DFS (tree[i][1], layer + 1); 
}

int main () {
	int n;
	CIN >> N;
	for (int i = 1; I <= n; i++) {
		cin >> tree[i][0] >> tree[i][1];
	}
	Layernum = 0;
	DFS (1, 1);
	for (int i = 1; I <= n; i++) {
		if (Width[i] > Widthnum) widthnum = Width[i];
	cout << widthnum << "" << layernum;
	return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.