题目描述:

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 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
样例输出:
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;
else
flag = 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;
else
cout << "Case " << Case << " is not a tree." << endl;
}
}

  

九度OJ - 题目1481:Is It A Tree?的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. Installing MySQL Connector/Python using pip v1.5

    The latest pip versions will fail on you when the packages it needs to install are not hosted on PyP ...

  2. [Java]java反射随笔

    类是面向对象的灵魂,一切事物都可以以类来抽象. 在java使用过程中,我们可能会经常用到一个反射的知识,只是别人都封装好的,如jdbc的加载驱动类有一句Class.for(“…jdbc…”).newI ...

  3. [转]利用vertical-align:middle实现在整个页面居中

    本文转自:http://www.cnblogs.com/xueming/archive/2012/03/21/VerticalAlign.html 如果想让一个div或一张图片相对于整个页面居中,用v ...

  4. 关于简明Vim练级攻略

    本文装载于:http://coolshell.cn/articles/5426.html vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你 ...

  5. 【memset】关于memset的初始最大最小值

    声明:也是摘自网上各路大神的. memset的正规用法是只能用来初始化char类型的数组的,也就是说,它只接受0x00-0xFF的赋值. 因为char是1字节,memset是按照字节赋值的,相当于把每 ...

  6. win2003域控制器密码遗忘如何修改

    在公司遇到这么个事儿,员工搭建QC服务器,设置了域账户登陆系统.但忘记了登录密码,使用PE直接修改sam文件不好用.   1.使用PE进系统修改组登陆方式的账号administrator密码 需符合复 ...

  7. Entity Framework 6.1-Model First

    原文:Entity Framework 6.1-Model First Model First-顾名思义,就是先创建EF数据模型,通过数据模型生成数据库的EF创建方式. 步骤. 1.新建一个DAL的文 ...

  8. eclipse插件hibernate tools安装 爱好者

    eclipse helios(3.6)版 1.启动eclipse 2.选择Help > Install New Software...> 3.添加如下地址:http://download. ...

  9. (转)Hessian(C#)介绍及使用说明

    什么是Hessian? Hessian是Caucho开发的一种二进制Web Service协议.支持目前所有流行的开发平台. Hessia能干什么? hessian用来实现web服务. Hessia有 ...

  10. 【wenqi】重置Centos 7 Root密码

    重置Centos 7 Root密码的方式和Centos 6完全不同.下面展示一下到底如何操作. 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux ...