A - Is It A Tree?

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

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。 问题分析:题目要求判断是否为树,先理清是树的条件,树是一种无向简单连通图
1.无回路和环 也就是说(1,2)(2,1)或者(1,1)或者(1,5)(1,5)这些情况都是不行的。
2.要点与点之间连通,而且注意空树(即无点无边的树)也是树。
 #include "cstdio"
struct node
{
int i;
int j;
};
node a[],b[];
int number,time=;
int nbegin()
{
int i=;
time++;
while (scanf ("%d%d",&a[i].i,&a[i].j)&& (a[i].i != - || a[i].j != -))
{
number = i;
if (a[i].i || a[i].j)
i++;
else
return ;
}
return ;
}
void tree()
{
int now,next,q;
node y;
if (a[number].i == && a[number].j == && number == )
{
printf ("Case %d is a tree.\n",time);
return;
}
for (int i=;i<number;i++)
{
for (int k=;k<number;k++)
{
if (a[i].j == a[k].j && i != k)
{
printf("Case %d is not a tree.\n",time);
return;
}
if (a[i].i == a[k].j && a[i].j == a[k].i)
{
printf("Case %d is not a tree.\n",time);
return;
}
}
}
for (int i=;i<number;i++)
{
now=;
next=;
q=;
for (int t=;t<number;t++)
if (a[t].i == a[i].i )
{
b[next++] = a[t];
q++;
}
while (next != now)
{
y = b[now];
now++;
for (int j=;j<number;j++)
if (y.j == a[j].i && y.i != a[j].j)
{
b[next++] = a[j];
q++;
}
}
if (q == number)
{
printf ("Case %d is a tree.\n",time);
return;
}
}
printf("Case %d is not a tree.\n",time);
}
int main()
{
while(nbegin())
tree();
return ;
}

暑假集训(2)第一弹 -----Is It A Tree?(Poj308)的更多相关文章

  1. 暑假集训(4)第一弹 -----递推(Hdu2039)

    题意梗概:fff团团员小A想退团了,不过要退团,他必须绘制出贤者法阵,以证明他有资格不受大fff之灵的监督 并退团,小A他现在要开始收集材料了,但是,他不清楚应该买多少份材料. 虽然你并不想帮他退团, ...

  2. 暑假集训(1)第一弹 -----士兵队列训练问题(Hdu1276)

    Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报 ...

  3. 暑假集训(5)第一弹——— Super Jumping! Jumping! Jumping!(hdu1087)

    题意概括:在上次与娑殚的三次博弈中,你们都取得了胜利.便向娑殚提出要求,借助他的力量,传送到一个安全的地方. 你们的愿望达成了,不过,你和小A似乎失散了. 街上人来人往的特别热闹,每一个人的脸上都洋溢 ...

  4. 暑假集训(3)第一弹 -----还是畅通工程(hdu1233)

    题意梗概:N(n<100)个村子想要富起来,自然就要先修路,不过到底还是没富起来,所以陷入了一个怪圈 :资金不足->修不起路->资金不足...... 为了实现走向全民小康社会,全面实 ...

  5. 暑假集训(3)第二弹 -----Jungle Roads(Hdu1301)

    问题梗概:自从上次某个acmer来设计了拉格瑞圣岛的交通路线后,岛上的酋长就相当苦恼,他发现,虽然这些修好的公路便利了岛上的 交通,并且让拉格瑞圣岛的旅游业更加兴旺,甚至他们还收到了一笔不小的国际资金 ...

  6. 暑假集训(4)第二弹 -----递推(hdu2254)

    题意概括:上次小A在你的帮助下成功炼成贤者法阵的第一部分——三角分隔,现在他准备绘制法阵的第二部分——莫测矩形. 而他又遇到了一个问题,他不知道不同矩形到底有多少个. 秉持帮人帮到底,送佛送到西的基本 ...

  7. 暑假集训(2)第二弹 ----- The Suspects(POJ1611)

    B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:20000KB   ...

  8. 暑假集训(5)第二弹———湫湫系列故事——减肥记I(hdu4508)

    问题描述:舔了舔嘴上的油渍,你陷在身后柔软的靠椅上.在德源大赛中获得优胜的你,迫不及待地赶到“吃到饱”饭店吃到饱.当你 正准备离开时,服务员叫住了你,“先生,您还没有吃完你所点的酒菜.”指着你桌上的一 ...

  9. 暑假集训(2)第九弹 ----- Points on Cycle(hdu1700)

                                                Points on Cycle Time Limit:1000MS     Memory Limit:32768 ...

随机推荐

  1. C语言之Static

    1.全局静态变量 在全局变量之前加上关键字static,全局变量就被定义成为一个全局静态变量. 1)内存中的位置:静态存储区(静态存储区在整个程序运行期间都存在) 2)初始化:未经初始化的全局静态变量 ...

  2. storm出现的背景

     storm出现的背景 互联网从诞生的第一时间起,对世界的最大改变就是让信息能够实时交互,从而大大加速了各个环节的效率.正因为大家有对信息实时响应.实时交互的需求,所以软件行业除了个人操作系统之外,数 ...

  3. 简单计算器 (c语言课程设计)

    可以实现简单的加减乘除四则运算 #include<stdio.h> #include<string.h> #define MAX 10100 int main() { int ...

  4. Spring源码入门——XmlBeanDefinitionReader解析

    接上篇[] ,我们看到BeanDefinitionReader解决的是从资源文件(xml,propert)到BeanDefinition集合的过程.所以BeanDefinitionReader接口有两 ...

  5. javascript中使用md5函数

    javascript中使用md5函数 这对于js来讲本来是没有的,现在可以自己定义一个md5的函数,达到加密效果. var hexcase = 0; function hex_md5(a) { if ...

  6. Cactus入门

    这是一个WebProject,有关Cactus用法详见本文测试用例 首先是web.xml <?xml version="1.0" encoding="UTF-8&q ...

  7. Spring mvc Interceptor 解决Session超时配置流程

    最近公司内部框架中对Session超时这一功能未实现,由于采用iframe结构,Session超时后,当点击左侧系统菜单时,会在iframe的右侧再次弹出登陆框. 该问题是由于没有设置拦截器造成. 添 ...

  8. UEFI引导修复教程和工具

    参考 http://bbs.wuyou.com/forum.php?mod=viewthread&tid=323759 1. MBR分区表:Master Boot Record,即硬盘主引导记 ...

  9. 写一些有关android的东西吧,那时候玩android时候的一些笔记

    写一些有关android的东西吧,那时候玩android时候的一些笔记

  10. jrae源代码解析(二)

    本文细述上文引出的RAECost和SoftmaxCost两个类. SoftmaxCost 我们已经知道.SoftmaxCost类在给定features和label的情况下(超參数给定),衡量给定权重( ...