HDU 1325 Is It A Tree?(树的判断,经典题目)
来源:互联网 发布:天刀捏脸数据 女 冰儿 编辑:程序博客网 时间:2024/06/05 02:58
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21682 Accepted Submission(s): 4885
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.
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 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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.
Source
North Central North America 1997
题意:
应该很清楚的题意了,就是让判断是不是一棵树。
思路:
小希的迷宫(点击查看),简单修改后提交,超时!超时!用数组记录节点入度,超时......最后看了题解,参考了宇神的博客。
参考:CSDN 笑着走完自己的路
代码:
#include<stdio.h>#define MYDD 1103int pre[MYDD];//记录根节点int indeg[MYDD];//节点入度 degreevoid init() {for(int j=0; j<MYDD; j++) {pre[j]=0;//根节点此时不能够是自身 indeg[j]=0;//入度初始化为 0 }}int find(int x) {return x==pre[x]? x:find(pre[x]);}void combine(int x,int y) {int fx=find(x);int fy=find(y);if(fx!=fy)pre[fx]=fy;}int main() {int a,b,v=1;while(scanf("%d%d",&a,&b)&&(a>=0&&b>=0)) {init(); int flag=0;//判断是否存在环while(a||b) {if(pre[a]==0)pre[a]=a;if(pre[b]==0)pre[b]=b;indeg[b]++;if(find(a)==find(b))flag=1;//有同一个根节点,存在环combine(a,b);scanf("%d%d",&a,&b);}if(flag) {printf("Case %d is not a tree.\n",v++);} else { //继续判断连通性int sum=0;//记录树个数flag=0;for(int j=1; j<MYDD; j++) {if(pre[j]==j) {sum++;//根节点是本身if(sum>1) {flag=1;//没有连通break;}}if(indeg[j]>1) {//入度大于 1 flag=1;break;}}if(flag) {printf("Case %d is not a tree.\n",v++);} elseprintf("Case %d is a tree.\n",v++);}}return 0;}
后:
判断一棵树的方法:
只有一个根节点, n 个节点 n-1 条边,根节点的入度 0 ,其他每个节点的入度 1 ,不存在连通分支。
*****************************************************************************************************************
0 0
- HDU 1325 Is It A Tree?(树的判断,经典题目)
- HDU:1325 Is It A Tree?(并查集+有向图树的判断)
- HDU 1325 Is It A Tree?(并查集/有向树的判断)
- HDU 1325 Is It A Tree?(判断是否为树)
- HDU 1325 Is It A Tree? ---树
- hdu 1325 Vs hdu 1272 is it a tree ? 并查集:对是否是树的判断
- hdu 1325 Is It A Tree?(利用入度判断有向树)
- hdu 1325 Is It A Tree? 判断是否是树 巨坑
- HDU 1325 Is It A Tree? 并查集判断是否成树
- HDU 1325&&POJ 1308 Is It A Tree? 并查集判断能否树(能否形成环)
- hdu1325 Is It A Tree?(二叉树的判断)
- Is it a tree(判断是否是树)
- HDU 1325 Is It a Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- 心少朴
- 11-23 多媒体 MediaPlayer、Camera、MediaRecorder Vitamio 笔记
- 什么是OGNL表达式
- 关于MySql8小时空闲后连接超时问题(testOnBorrow,logAbandoned等)
- tjut 4333
- HDU 1325 Is It A Tree?(树的判断,经典题目)
- 每周学一点Egret(15) 桌面版龙骨预览工具
- 设计模式六大原则
- [php学习十七]JQuery练习2-动画
- java集合---hashcode和equals的重写
- 共同学习Java源代码--数据结构--ArrayList类(八)
- HDOJ-----2120并查集(水题)
- 广告页右上角的“跳过”按钮
- 中国剩余定理(CRT)学习小结