题目描述:

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.

输入:

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 and less than 10000.

输出:

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).

样例输入:
6 8  5 3  5 2  6 4
5 6  0 08 1  7 3  6 2  8 9  7 5
7 4  7 8  7 6  0 03 8  6 8  6 4
5 3  5 6  5 2  0 0
-1 -1
样例输出:
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
来源:
2012年北京大学计算机研究生机试真题
#include <iostream>
using namespace std;const int N = 10000;int set[N];
bool visited[N];int find(int t) {if (set[t] == t)return t;return set[t] = find(set[t]);
}int main() {int Case = 0, a, b;while (cin >> a >> b && a >= 0 && b >= 0) {bool flag = true;int edge = 0, node = 0;Case++;for (int i = 0; i < N; i++) {set[i] = i;visited[i] = false;}while (a != 0 && b != 0) {if (find(b) == b && find(a) != b)set[b] = a;elseflag = false;if (!visited[a]) {visited[a] = true;node++;}if (!visited[b]) {visited[b] = true;node++;}edge++;cin >> a >> b;}if (node != 0 && node != edge + 1)flag = false;if (flag)cout << "Case " << Case << " is a tree." << endl;elsecout << "Case " << Case << " is not a tree." << endl;}
}

  

转载于:https://www.cnblogs.com/qwertWZ/p/3565827.html

九度OJ - 题目1481:Is It A Tree?相关推荐

  1. 九度OJ 题目1179:阶乘

    /********************************* * 日期:2013-2-8 * 作者:SJF0115 * 题号: 九度OJ 题目1179:阶乘 * 来源:http://ac.jo ...

  2. 九度OJ 题目1069:查找学生信息 随笔

    ** 九度OJ 题目1069:查找学生信息 ** 题目描述如下: 输入N个学生的信息,然后进行查询. 输入 输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息 ...

  3. 九度OJ 题目1203:IP地址

    /********************************* * 日期:2013-2-8 * 作者:SJF0115 * 题号: 九度OJ 题目1203:IP地址 * 来源:http://ac. ...

  4. 九度OJ—题目1032:ZOJ

    题目描写叙述: 读入一个字符串.字符串中包括ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出.当某个字符用完时,剩下的仍然依照ZOJ的顺序输出. 输入: 题目包括多组用例,每组用例占一行,包括ZOJ ...

  5. 九度oj 题目1411:转圈

    题目描述: 在一个有向图有n个顶点(编号从1到n),给一个起点s,问从起点出发,至少经过一条边,回到起点的最短距离. 输入: 输入包括多组,每组输入第一行包括三个整数n,m,s(1<=n< ...

  6. 【WA】九度OJ题目1435:迷瘴

    题目描述: 通过悬崖的yifenfei,又面临着幽谷的考验-- 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅.由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身溃烂而死. 幸好y ...

  7. 九度oj 题目1354:和为S的连续正数序列

    题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久, ...

  8. 九度oj 题目1376:最近零子序列

    题目描述: 给定一个整数序列,你会求最大子串和吗?几乎所有的数据结构与算法都会描述求最大子串和的算法.今天让大家来算算最近0子串和,即整数序列中最接近0的连续子串和.例如,整数序列6, -4, 5, ...

  9. 九度oj 题目1078:二叉树遍历

    题目1078:二叉树遍历 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5326 解决:3174 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历 ...

  10. 九度oj 题目1374:所有员工年龄排序

    题目描述: 公司现在要对所有员工的年龄进行排序,因为公司员工的人数非常多,所以要求排序算法的效率要非常高,你能写出这样的程序吗? 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为一 ...

最新文章

  1. Regular Expression Matching
  2. 【黑客浅析】像黑客一样思考
  3. opencv转pytorch
  4. RIP 注意事项、学习要点及其总结
  5. 什么是真正的APM?
  6. 自动机理论、形式语言和计算导论提纲
  7. Kernel panic -not syncing:Attempted to kill init!
  8. 前端学习(1158):对象结构01
  9. S3C2440上LCD驱动(FrameBuffer)实例开发讲解
  10. 分枝定界法解0/1背包问题
  11. THREEJS - 获取场景中模型数据
  12. 复利计算1.0-2.0-3.0
  13. lorawan服务器通信协议,LoRaWAN协议(三)–Server端数据协议
  14. 高清加载具体(如清明上河图)
  15. plupload文件上传插件
  16. CardView介绍
  17. 2021美赛C题解题思路(Confirming the Buzz about Hornets)
  18. 刚刚,北大智能学院成立!AI视觉泰斗朱松纯教授任院长
  19. 鸿蒙小游戏-俄罗斯方块
  20. 可拖拽的弹出窗口Js插件

热门文章

  1. 建武28a对讲机最大距离_小米对讲机再出新品:不但价格低,还能一键写频
  2. 安装nvidia-docker
  3. asscalar()函数
  4. SLIC算法分割超像素原理及Python、C++
  5. 强烈推荐一个在线caffe网络可视化工具!!
  6. ubuntu下caffe的FCN8模型训练
  7. 基于YOLO的目标检测系统实践
  8. 考研算法语言C,2017年华南理工大学硕士研究生算法语言(C)考试大纲
  9. HyperLedger Composer 查看所有容器 | 进入指定容器
  10. Linux学习(3)Linux文件的基本属性、文件内容查看