有根树的表达 Aizu - ALDS1_7_A: Rooted Trees
有根树的表达
题目:Rooted Trees Aizu - ALDS1_7_A
A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a binary relation on V represented by a set of edges. Fig. 1 illustrates an example of a graph (or graphs).
Fig. 1
A free tree is a connnected, acyclic, undirected graph. A rooted tree is a free tree in which one of the vertices is distinguished from the others. A vertex of a rooted tree is called "node."
Your task is to write a program which reports the following information for each node u of a given rooted tree T:
- node ID of u
- parent of u
- depth of u
- node type (root, internal node or leaf)
- a list of chidlren of u
If the last edge on the path from the root r of a tree T to a node x is (p, x), then p is the parent of x, and x is a child of p. The root is the only node in T with no parent.
A node with no children is an external node or leaf. A nonleaf node is an internal node
The number of children of a node x in a rooted tree T is called the degree of x.
The length of the path from the root r to a node x is the depth of x in T.
Here, the given tree consists of n nodes and evey node has a unique ID from 0 to n-1.
Fig. 2 shows an example of rooted trees where ID of each node is indicated by a number in a circle (node). The example corresponds to the first sample input.
Fig. 2
Input
The first line of the input includes an integer n, the number of nodes of the tree.
In the next n lines, the information of each node u is given in the following format:
id k c1 c2 ... ck
where id is the node ID of u, k is the degree of u, c1 ... ck are node IDs of 1st, ... kth child of u. If the node does not have a child, the k is 0.
Output
Print the information of each node in the following format ordered by IDs:
node id: parent = p , depth = d, type, [c1...ck]
p is ID of its parent. If the node does not have a parent, print -1.
d is depth of the node.
type is a type of nodes represented by a string (root, internal node or leaf). If the root can be considered as a leaf or an internal node, print root.
c1...ck is the list of children as a ordered tree.
Please follow the format presented in a sample output below.
Constraints
- 1 ≤ n ≤ 100000
Sample Input 1
13
0 3 1 4 10
1 2 2 3
2 0
3 0
4 3 5 6 7
5 0
6 0
7 2 8 9
8 0
9 0
10 2 11 12
11 0
12 0
Sample Output 1
node 0: parent = -1, depth = 0, root, [1, 4, 10]
node 1: parent = 0, depth = 1, internal node, [2, 3]
node 2: parent = 1, depth = 2, leaf, []
node 3: parent = 1, depth = 2, leaf, []
node 4: parent = 0, depth = 1, internal node, [5, 6, 7]
node 5: parent = 4, depth = 2, leaf, []
node 6: parent = 4, depth = 2, leaf, []
node 7: parent = 4, depth = 2, internal node, [8, 9]
node 8: parent = 7, depth = 3, leaf, []
node 9: parent = 7, depth = 3, leaf, []
node 10: parent = 0, depth = 1, internal node, [11, 12]
node 11: parent = 10, depth = 2, leaf, []
node 12: parent = 10, depth = 2, leaf, []
Sample Input 2
4
1 3 3 2 0
0 0
3 0
2 0
Sample Output 2
node 0: parent = 1, depth = 1, leaf, []
node 1: parent = -1, depth = 0, root, [3, 2, 0]
node 2: parent = 1, depth = 1, leaf, []
node 3: parent = 1, depth = 1, leaf, []
Note
You can use a left-child, right-sibling representation to implement a tree which has the following data:
- the parent of u
- the leftmost child of u
- the immediate right sibling of u
Reference
Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.
思路:
在写这个代码的时候,我其实是想用一个struct直接将这个树中的节点的信息全部包起来的(父节点,子节点,深度,节点类型)。但是,在敲的过程中发现,这是不可能的。或者说这会导致大量的空间的浪费,或许可以用vector试试,但是暂时没什么时间,就不去试试了。在看了挑战程序设计书上的思路之后,才发现原来这个树是可以用左子右兄弟表示法来表示。这样子,不仅可以非常便捷地将树表示出来,满足题目要求的输出也不会占用太长的代码,真是一个好的常规有根树的表示方法。
关于树的深度的判断,我是采用了递归的方法。首先,一定要找到这个树的根节点是哪一个节点,然后,在进入递归函数findde(int po,int h)。函数中的h一定是当前节点po的的深度,记录在de数组中。之后通过节点的右指针指向该节点的相邻的右兄弟的定义递归遍历树的这一层,最后递归遍历节点的子节点,深度加1。
void findde(int po,int h)
{
de[po]=h;
int ri=point[po].right;
int le=point[po].left;
if(ri!=-)
findde(ri,h);
if(le!=-)
findde(le,h+);
}
关于输出的话,其实就是注重一些格式的问题。不过有一点我想提一下,其实也是在敲代码的时候就出现的一点点问题。我一开始的时候用的是while循环来遍历这个节点的孩子。但是,会出现一些的格式问题,例如多了一点“, ”之类的,后来是用了两次判断l是否为-1来解决的问题,虽然是AC了,但是在while循环中其实是判断了两次的l是否为-1。这不是我希望的简洁的代码。于是我去看了一下书上的代码。惊为天人,原来还可以这样!他利用了for循环的特点完美地解决了我的问题,果然大佬就是大佬啊!
//我的子节点的遍历输出
int l=point[i].left;
while(l!=-)
{
cout<<l;
l=point[l].right;
if(l!=-)
cout<<", ";
}
cout<<"]"<<endl;
//大佬的子节点的遍历输出
for(int j=,c=point[i].left;c!=-;j++,c=point[c].right)
{
if(j) cout<<", ";
cout<<c;
}
cout<<"]"<<endl;
AC代码:
#include <iostream>
#include <string>
#include <cstring> using namespace std; struct Node
{
int pa;
int left; //表示的是该节点的左子节点
int right; //表示的是该节点的第一个右兄弟
};
Node point[];
int de[];
int n; void findde(int po,int h)
{
de[po]=h;
int ri=point[po].right;
int le=point[po].left;
if(ri!=-)
findde(ri,h);
if(le!=-)
findde(le,h+);
} void print()
{
for(int i=; i<n; i++)
{
cout<<"node "<<i<<": parent = "<<point[i].pa<<", depth = "<<de[i]<<", ";
if(point[i].pa==-)
cout<<"root, [";
else if(point[i].left==-)
cout<<"leaf, [";
else
cout<<"internal node, [";
//我的子节点的遍历输出
// int l=point[i].left;
// while(l!=-1)
// {
// cout<<l;
// l=point[l].right;
// if(l!=-1)
// cout<<", ";
// }
// cout<<"]"<<endl;
//大佬的子节点的遍历输出
for(int j=,c=point[i].left;c!=-;j++,c=point[c].right)
{
if(j) cout<<", ";
cout<<c;
}
cout<<"]"<<endl;
}
}
void init()
{
for(int i=;i<n;i++)
{
point[i].pa=-;
point[i].left=-;
point[i].right=-;
}
}
int main()
{
cin>>n;
memset(de,,);
init();
for(int i=; i<n; i++)
{
int a,num,l;
cin>>a>>num;
if(num)
{
cin>>l;
point[a].left=l;
point[l].pa=a;
}
for(int j=; j<num; j++)
{
int x;
cin>>x;
point[l].right=x;
point[x].pa=a;
l=x;
}
}
int root=-;
for(int i=; i<n; i++)
if(point[i].pa==-)
root=i;
findde(root,);
print();
return ;
}
总结:
这一次敲的代码反映了我的一些问题,如:敲代码之前总是不先想清楚需要的空间/定义的变量,导致写着写着忽然发现自己的想法好像不能实现,之后修改思路,这非常浪费时间。在一些细节方面,总是容易忽略,这就会导致我总是WA在细节上(虽然这一次没有),但是也在输入时候设置节点的双亲节点,子节点,兄弟节点的地方卡住了好一会。也可能是我自己的逻辑思维还是不够严密吧!加油呀!
有根树的表达 Aizu - ALDS1_7_A: Rooted Trees的更多相关文章
- 【Aizu - ALDS1_7_A】Rooted Trees(树的表达)
Rooted Trees Descriptions: A graph G = (V, E) is a data structure where V is a finite set of vertice ...
- Tree - Rooted Trees
Rooted Trees A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a b ...
- HDU p1294 Rooted Trees Problem 解题报告
http://www.cnblogs.com/keam37/p/3639294.html keam所有 转载请注明出处 Problem Description Give you two definit ...
- 10.3 Implementing pointers and objects and 10.4 Representing rooted trees
Algorithms 10.3 Implementing pointers and objects and 10.4 Representing rooted trees Allocating an ...
- HDU1294 Rooted Trees Problem(整数划分 组合数学 DP)
讲解见http://www.cnblogs.com/IMGavin/p/5621370.html, 4 可重组合 dfs枚举子树的节点个数,相乘再累加 1 #include<iostream& ...
- HDU 1294 Rooted Trees Problem
题目大意:求有n个节点的树有几种? 题解:http://www.cnblogs.com/keam37/p/3639294.html #include <iostream> typedef ...
- [LeetCode] 310. Minimum Height Trees 解题思路
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
随机推荐
- 使用bootstrap制作网站导航
除了制作选项卡和下拉菜单,bootstrap还能编写出美观的网站导航栏 一.仿知乎导航栏 <body> <nav class="navbar navbar-default ...
- FZU 2187 回家种地 ( 扫描线 + 离散 求矩阵单次覆盖面积 )
2187 回家种地 Accept: 56 Submit: 230Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Descript ...
- 阿里云云监控agent插件 - Linux版
阿里云云监控agent插件使用指南 1.安装(注意,要以“root”权限运行,复制 sudo后面的就行,别把#也复制进去) #64位 # sudo bash -c "wget -e 'htt ...
- Go语言_包、变量和函数
包.变量和函数 学习 Go 程序的基本结构. Go 作者组编写,Go-zh 小组翻译. https://go-zh.org 包 每个 Go 程序都是由包构成的. 程序从 main 包开始运行. 本程序 ...
- C#面试 笔试题 四
1.请你简单的说明数据库建立索引的优缺点 使用索引可以加快数据的查询速度,不过由于数据插入过程中会建索引,所以会降低数据的插入.更新速度,索引还会占磁盘空间. 2.什么是WEB服务控件?使用WEB服务 ...
- 暂时放弃ts版个人博客转js版博客
我本打算信心满满的做个vue+ts做个博客的,其实架构搭的差不多了,但是我在用vuex的时候发现一个自己无法忍受的瑕疵,那就是在用vuex的时候,得利于普通版vuex的map语法糖实在太好用,这把我惯 ...
- 纯手动拉WebPanel页面保存出现错误提示 "error:字符文本中的字符太多"
环境为.Net 没有使用WorkWithPlus 纯手拉WebFrom页面 问题出现情况如下:在拉页面的过程中拖了3个Value类型的变量到Table中 页面如下 但是在我保存的时候 发现提示如下错 ...
- 使用HystrixCommand封装http请求
1.引入依赖 要排除hystrix-core里的archaius-core,否则报错 <dependency> <groupId>com.netflix.hystrix< ...
- 关于Jenkins的网站及其他学习的网站
配置efk https://www.cnblogs.com/fzxiaomange/p/efk-getstart.html https://blog.csdn.net/wangmuming/artic ...
- django报错
报错: SyntaxError Generator expression must be parenthesized 问题原因: 由于django 1.11版本和python3.7版本不兼容, 2.0 ...