(hdu step 5.1.2)Is It A Tree?(判断是不是一棵树)

题目:

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 850 Accepted Submission(s): 273
 

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.

 

Source

North Central North America 1997


Recommend

Ignatius.L

题目大意:

给出一堆边给你,让你判断这是不是一棵树。边的信息以(start , end)的形式给出.

题目分析:

这道题可以用并查集来做,也可以直接使用树的定义来做即可。以下给出使用树的定义来做的解题思路。

树的定义:

1)一个具有n个结点的数,最多只有n-1个结点

2)一棵树有且只有一个入度为0的结点,并且所有结点的入度都不大于1.

其中条件1)已经能有条件2)保证。所以在编码的时候,主要针对条件2)进行变么即可

代码如下:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

const int maxn = 1005;

struct Edge {//边
	int start;//起点
	int end;//终点
} edges[maxn];

int indegree[maxn];//某一个点的入度

/**
 * 判断一个队列q里面是否包含某一个数a
 */
bool isContain(queue<int> q,int a){

	while(!q.empty()){//如果该队列非空
		int temp = q.front();//则不断地遍历该队列
		q.pop();

		if(temp == a){//如果包含数a
			return true;//则返回true
		}
	}

	return false;
}

int main() {
	int a, b;
	int counter;
	int caseNum = 1;

	while (cin >> a >> b) {//注意这种数据的读取方式
		queue<int> q;//用来保存这个图中出现过的点

		if (a < 0 && b < 0) {
			break;
		}

		counter = 0;//初始化边数
		memset(indegree, 0, sizeof(indegree));//初始化每个点的入度.默认初始化为0

		while (a != 0 && b != 0) {
			if(isContain(q,a) == false){//如果q中还没有出现过a
				q.push(a);//则将a入队
			}

			if(isContain(q,b) == false){
				q.push(b);
			}

			edges[counter].start = a;//记录边的信息
			edges[counter].end = b;
			indegree[edges[counter].end]++;//记录每个点的入度
			counter++;

			cin >> a >> b;
		}

		bool flag = true;//用于标记这是否是一棵树
		int root = 0;//入度为0的点的个数

		while (!q.empty()) {
			int temp = q.front();
			q.pop();

			if (indegree[temp] == 0) {//如果改点的入度为0
				root++;//则入度为0的点的数目+1
			}

			if (indegree[temp] > 1) {//如果改点的入度>1
				flag = false;//则证明该图不是一棵树.(因为树中不存在入度>1的结点)
				break;
			}

		}

		if (root > 1) {//如果入度为0的结点的数目>1
			flag = false;//则证明该图不是一棵树
		}

		if (flag == true) {
			cout << "Case " << (caseNum++) << " is a tree." << endl;
		} else {
			cout << "Case " << (caseNum++) << " is not a tree." << endl;
		}
	}

	return 0;
}
时间: 2024-10-11 04:45:19

(hdu step 5.1.2)Is It A Tree?(判断是不是一棵树)的相关文章

(hdu step 2.1.2)How many prime numbers(判断一个数是否是质数)

题目: How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8513 Accepted Submission(s): 2716   Problem Description Give you a lot of positive integers, just to find out how many pr

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

(hdu step 1.3.1)FatMouse&#39; Trade(在收入需要一定的付出的情况下求最大收入)

题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5092 Accepted Submission(s): 1530   Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua

(hdu step 6.1.2)Eddy&#39;s picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)

题目: Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 172 Accepted Submission(s): 126   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be

(hdu step 5.1.1)A Bug&#39;s Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)

题目: A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 723 Accepted Submission(s): 277   Problem Description Background Professor Hopper is researching the sexual behavior of a rare spe

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble

(hdu step 3.1.5)Tiling_easy version(求用2*1、2*2两种骨格铺满2*n的网格的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Tiling_easy version Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 515 Accepted Submission(s): 447   Prob

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码..是不是有点浪费...但是我觉得似乎哪里也优化不了了.... 判断线段相交就是利用的叉积.假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交.此时需要用同样的方法去判断L2是否在L1的两个端点之间,如果L2也在L1的两个端点之间的话,那就足以说明L1与L2相交.但是这题还需要判断是否端点也相交