博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1325 Is It A Tree? 判断是否为一棵树
阅读量:6094 次
发布时间:2019-06-20

本文共 3559 字,大约阅读时间需要 11 分钟。

题目链接:

POJ同题:

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 12963    Accepted Submission(s): 2919

Problem Description

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. 
There is exactly one node, called the root, to which no directed edges point. 
Every node except the root has exactly one edge pointing to it. 
There is a unique sequence of directed edges from the root to each node. 
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not. 
 

 

Input

The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero. 
 

 

Output

For each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1). 
 

 

Sample Input

6 8 5 3 5 2 6 4 5 6 0 0
8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0
3 8 6 8 6 4 5 3 5 6 5 2 0 0
-1 -1
 

 

Sample Output

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
 
分析:此题纯属判断一个图是否满足一棵树,输入整得好坑爹- -!。
树的性质:空树或一个根节点、有向无环、除根结点外每个结点入度都为1。
注意,有环的图的情况:要么不存在入度为0的结点,要么存在入度大于1的点。
这样根据以上性质进行编程,如下:
1 #include 
2 #include
3 #include
4 using namespace std; 5 6 const int maxn = 10010; 7 bool vis[maxn]; 8 int indeg[maxn]; 9 int main()10 {11 int cas = 0;12 int a, b;13 while(scanf("%d%d", &a, &b),a>=0||b>=0)14 {15 if(a==0 && b==0)16 {17 printf("Case %d is a tree.\n", ++cas);18 continue;19 }20 int maxx = 0;21 if(a > maxx) maxx = a;22 if(b > maxx) maxx = b;23 memset(vis, false, sizeof(vis)); //该结点是否存在24 memset(indeg, 0, sizeof(indeg));25 vis[a] = vis[b] = true; //设置结点存在26 indeg[b]++; //入度+1,注意是有顺序的27 while(scanf("%d%d", &a, &b), a!=0||b!=0)28 {29 if(a > maxx) maxx = a;30 if(b > maxx) maxx = b;31 indeg[b]++; //入度+132 vis[a] = vis[b] = true; //设置结点存在33 }34 bool flag = true;35 int k = 0;36 for(int i = 1; i <= maxx; i++)37 if(vis[i] && indeg[i] > 1) //入度大于1,说明一个结点有两个父结点,违背树的性质38 {39 flag = false;40 break;41 }42 for(int i = 1; i <= maxx; i++)43 if(vis[i] && indeg[i] == 0) //入度等于0的点有两个以上(包括)的话,则说明根结点不唯一,可能是森林44 k++;45 if(!flag || k != 1)46 printf("Case %d is not a tree.\n", ++cas);47 else48 printf("Case %d is a tree.\n", ++cas);49 }50 return 0;51 }
View Code

 

转载于:https://www.cnblogs.com/dzkang2011/p/tree_1.html

你可能感兴趣的文章
四种简单的排序算法(转)
查看>>
Quartz2D之着色器使用初步
查看>>
多线程条件
查看>>
Git [remote rejected] xxxx->xxxx <no such ref>修复了推送分支的错误
查看>>
Porter/Duff,图片加遮罩setColorFilter
查看>>
黄聪:VMware安装Ubuntu10.10【图解】转
查看>>
Centos 6.x 升级openssh版本
查看>>
公式推♂倒题
查看>>
vue实现点击展开,点击收起
查看>>
如何使frame能居中显示
查看>>
第k小数
查看>>
构建之法阅读笔记三
查看>>
Python/PHP 远程文件/图片 下载
查看>>
【原创】一文彻底搞懂安卓WebView白名单校验
查看>>
写给对前途迷茫的朋友:五句话定会改变你的人生
查看>>
并行程序设计学习心得1——并行计算机存储
查看>>
mysql练习题40道
查看>>
JAVA入门到精通-第86讲-半双工/全双工
查看>>
bulk
查看>>
js document.activeElement 获得焦点的元素
查看>>