hdoj1325Is It A Tree?
来源:互联网 发布:重庆大学网络教育专科 编辑:程序博客网 时间:2024/06/05 02:31
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17955 Accepted Submission(s): 4020
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.
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.
题目要求: 1 .必须有一棵树
2.不能成环!
3.而且要找出节点的上下限!
代码:#include<stdio.h>int pre[100050];int find(int p)//查找根节点 { int t; while(p!=pre[p]) p=pre[p]; return p;}int merge(int x,int y)//合并 { int fx=find(x); int fy=find(y); if(fx!=fy) { return 1; } else return 0;}int main(){ int n=1,m,a,b,u; int i,j; int v[100050]; while(scanf("%d%d",&a,&b)!=EOF&&(a>=0||b>=0)) { u=1; if(a==0&&b==0)//小心 ,有坑!! { printf("Case %d is a tree.\n",n); n++; continue; } for(i=0;i<=25;i++) { pre[i]=i; v[i]=0;//初始化标记数组 } int min=9999999,max=-1;//寻找最大和最小节点 while(a||b) { if(a<min) min=a; if(b<min) min=b; if(a>max) max=a; if(b>max) max=b; v[a]=1;v[b]=1;//标记 if(!merge(a,b))//判断是否是环 { u=0; } else pre[b]=a; scanf("%d%d",&a,&b); } m=0; if(u==0) printf("Case %d is not a tree.\n",n); else for(i=min;i<=max;i++) { if(v[i]&&pre[i]==i)//判断根的个数 m++; } if(m==1) printf("Case %d is a tree.\n",n); if(m>1) printf("Case %d is not a tree.\n",n); n++; } return 0;}原题目:hdoj1325Is It A Tree?
0 0
- hdoj1325Is It A Tree?
- hdoj1325Is It A Tree?(并查集)
- Is It A Tree?
- hdu1325Is It A Tree?
- Is it a tree?
- poj1308Is It A Tree
- poj1308Is It A Tree?
- BIT1060Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- hdu1325Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- IS IT A tree
- [Books]《Javascript权威指南》-- #6~12
- 最好的10个移动 Web 应用程序开发框架
- Java中的equals和==异同,你知道多少?
- React Native 简介:用 JavaScript 搭建 iOS 应用 (1)
- ImageLoader框架使用步骤
- hdoj1325Is It A Tree?
- tableView的编辑
- 【HDU5371】Hotaru's problem(Manacher + set)
- Singapore National Day (SG50 Golden Jubilee)
- 如何使用1M的内存排序100万个8位数
- ORM进阶:Hibernate的优劣对比
- 开始使用 Spring Data JPA
- 数据结构实验之链表六:有序链表的建立
- 2.3 寻找"水贴王"