这个是一个非常恶心的题,刚开始以为是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. QQ消息群发助手(超级简单)

    大家好!我开发的小工具,效果见动图: 下载地址 QQ群发助手.rar 代码: def Login(): global Flag try: bot.Login(['-q',v1.get()]) tkin ...

  2. 管理Exchange Online用户介绍(一)

    一.管理收件人 1.在“Office 365管理中心”主页,依次选择用户->添加用户 2.输入相关信息,其中包括名称.用户名.电子邮件地址等信息. 二.Exchange Online对用户邮箱的 ...

  3. F5 BIG-IP之二 LTM实验一

  4. why rpc

    why rpc 单体应用业务复杂, 大集群部署时对数据库的连接是个考验, 维护这个应用也比较费劲(一群人维护,容易代码冲突) 拆分后 各服务之间的接口依赖不能使用httpClient来搞,一是效率太低 ...

  5. 吴裕雄--天生自然 HADOOP大数据分布式处理:使用WinSCP连接本机与虚拟机

  6. OpenCV 亚像素级的角点检测

    #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #i ...

  7. cs231n spring 2017 lecture4 Introduction to Neural Networks

    1. Backpropagation:沿着computational graph利用链式法则求导.每个神经元有两个输入x.y,一个输出z,好多层这种神经元连接起来,这时候已知∂L/∂z,可以求出∂L/ ...

  8. ServletContextListener 监听器

    Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码.做一些业务逻辑判断等.其工作原理是,只要你在web.xml文件配置好要 ...

  9. Java IO: Reader And Writer

    原文链接 作者: Jakob Jenkov  译者: 李璟(jlee381344197@gmail.com) Java IO的Reader和Writer除了基于字符之外,其他方面都与InputStre ...

  10. JS Style Guide_1

    当你在回调函数里要使用函数表达式时,尽量使用箭头函数,比如数组中的 Map.filter.reduce等的回调函数中 [1,2,3].map((x) => { let y = x + 1; re ...