这个是一个非常恶心的题,刚开始以为是POJ的题,结果发现不是,一直都是超时。上题吧

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.
 
 
#include<stdio.h>
#include<string.h>
const int INF=;
int par[INF],ran[INF],sum;
bool t;
int find(int x)
{
if(par[x]==x)
return x;
return par[x]=find(par[x]);
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
par[x]=y;
else
t=true;
}
int main()
{
int x,y,m=;
while()
{
memset(par,,sizeof(par));
memset(ran,,sizeof(ran));
t=false;
while(scanf("%d%d",&x,&y)&&(x||y))
{
if(x<&&y<)
return ;
if(par[x]==)
par[x]=x;
if(par[y]==)
par[y]=y;
unite(x,y);
ran[y]++;
}
if(t)
printf("Case %d is not a tree.\n",m++);
else
{
int sum=,num=;
for(int i=; i<INF; i++)
{
if(par[i]==i)
{
sum++;
if(sum>)
{
num=;
break;
}
}
if(ran[i]>)
{
num=;
break;
}
}
if(num)
printf("Case %d is not a tree.\n",m++);
else
printf("Case %d is a tree.\n",m++);
}
}
return ;
}
 

HUD Is It A Tree?!!!!!)的更多相关文章

  1. debian、ubuntu:使用apt包管理器可能存在的问题! 让新手望而却步!

    apt包管理器说好真好,说不好真不好. 最近在debian9.ubuntu18.04上安装oracle 10g 玩. 怎么都准备不好安装环境.原因就是i386构架体系的deb包总安装不正确! baid ...

  2. js 也来 - 【拉勾专场】抛弃简历!让代码说话!

    前些日子谢亮兄弟丢了一个链接在群里,我当时看了下,觉得这种装逼题目没什么意思,因为每种语言都有不同的实现方法,你怎么能说你的方法一定比其他语言的好,所以要好的思路 + 好的语言特性运用才能让代码升华. ...

  3. 《Javascript DOM编程艺术》 读书笔记 —— 好书,通俗易懂!!!!! 相当的严谨!!!!

    1.javascript弱类型语言,解释性语言. 2.javascript数据类型:字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空( ...

  4. 一天完成把PC网站改为自适应!原来这么简单!

    http://www.webkaka.com/blog/archives/how-to-modify-a-web-page-to-be-responsive.html 一天完成把PC网站改为自适应!原 ...

  5. 【Mood-8】IT男!五更天!

    男人从毕业到30岁之间所承受的叠加的压力,赡养父母.结婚生子.升职加薪.工作压力.生活质量,这些东西可以压得我们喘不过起来,也成为我们成长的动力, 我们累,但不说累,我们闷着头做事儿,不张扬,但每个都 ...

  6. Asp.Net读取服务器EXE文件的方法!(超简单实例)

    Asp.Net读取服务器EXE文件的方法!(超简单实例) Process process = new Process(); process.StartInfo.FileName = "d:\ ...

  7. 摇滚吧HTML5!有声前端交互!(Hello, Jsonic!)

    软件工程师们摆弄1和0编写他们的乌托邦,音乐人门把玩12平均律上的音符构筑他们的伊甸园.最近,我偶然看了<蓝色骨头>这部电影,片中的男主角是位黑客,同时又兼具音乐创作的才华.在现实生活中, ...

  8. genymotion 模拟器 真是牛叉了 速度超快啊!!! 不解释了!建议大家速度去体验一把吧!

    已经有人写了blog了 我就不再赘述了,详情去这里看去吧!!   android genymotion模拟器怎么使用以及和google提供的模拟器性能对比  http://blog.csdn.net/ ...

  9. 【百度地图API】交你如何用百度地图搜索自己的数据!不需数据库!

    原文:[百度地图API]交你如何用百度地图搜索自己的数据!不需数据库! 摘要: 我有一定的房产数据,还有银行数据.我想在百度地图上标注出来,并且能搜索到我这些数据. 可是百度的数据库上并没有我的数据. ...

  10. Hello world! My first blog!

    Hello world! My first blog!

随机推荐

  1. ddt-python测试数据驱动工具(转载)

    背景 python 的unittest 没有自带数据驱动功能. 所以如果使用unittest,同时又想使用数据驱动,那么就可以使用DDT来完成. DDT是 “Data-Driven Tests”的缩写 ...

  2. linux 有了源码创建git版本库(coding)

    进入目录,比如ewei_shop 执行 git init 瞬间Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository),当前目录下多了一个.git的目录,如果没有看到 ...

  3. php启动后netstat看不到9000端口的问题

    https://www.cnblogs.com/jonsea/p/5522018.html     php-fpm配置文件详解 其实就是PHP配置文件改一个参数 listen = 127.0.0.1: ...

  4. 1)session总结

    (1)session的由来: HTTP协议是一种无状态协议,服务器响应完之后就失去了与浏览器的联系,最早,Netscape将cookie引入浏览器,使得数据可以客户端跨页面交换,那么服务器是如何记住众 ...

  5. Redis实现高并发分布式锁

    分布式锁场景在分布式环境下多个操作需要以原子的方式执行首先启一个springboot项目,再引入redis依赖包: <!-- https://mvnrepository.com/artifa . ...

  6. python3多线程应用详解(第三卷:图解多线程中join,守护线程应用)

  7. SHELL用法六(Find语句)

    1.SHELL编程Find语句案例实战 1)SHELL编程四剑客工具:Find.Grep.Sed.Awk,通过四剑客可以完成常规Linux指令无法完成或者比较复杂的功能,学好SHELL编程四剑客有助于 ...

  8. Java日期时间API系列12-----Jdk8中java.time包中的...

    package com.xkzhangsan.time.test; import java.time.LocalDateTime;import java.util.Date; import com.x ...

  9. Service层在MVC框架中的意义和职责

    https://blog.csdn.net/u012562943/article/details/53462157 mvc框架由model,view,controller组成,执行流程一般是:在con ...

  10. 三分钟学会使用Derby数据库

    Derby数据库是一个纯用Java实现的内存数据库,属于Apache的一个开源项目.由于是用Java实现的,所以可以在任何平台上运行:另外一个特点是体积小,免安装,java1.6开始集成了derby数 ...