hdu 1325 Is It A Tree?(并查集)

简介:

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 421 Accepted Submission(s): 157

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

同见poj(1038)

程序分析(转载):

  这道题跟小希的迷宫有很大的相似吧,只是一个是无向图一个是有向图。也是给你那些结点之间的信息,然后让你判断是不是一颗树罢了,用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环。只有一个入度为0的结点,不存在入度大于1的结点。这些也足以判断是否为一棵树了吧。不过要注意 一些特殊数据的情况,空树也是树。比如输入0 0。

解决方法:

  其实也可以不用并查集,这样就可以直接按照上面的条件来统计,就可以判断是不是一颗树了。

自我感言:额,这道题目关键就是判断树的那几个标准,1,无环;2,除了根,所有的入度为1,根入度为0;3,这个结构只有一个根,不然是森林了。

这是三个标准拿捏好,再注意这里空树也是树。基本上就ok了。。

坑爹的是下边的代码可以在hdu上通过,而不能在poj上通过。。。。。看了discuss才知道 1 1 0 0 不能是树。

下面几种情况要注意了。。。。

1: 0 0 空树是一棵树
2: 1 1 0 0 不是树 不能自己指向自己
3: 1 2 1 2 0 0 不是树....自己开始一直在这么WA  好郁闷 重复都不行呀~~5555
4: 1 2 2 3 4 5 不是树  森林不算是树(主要是注意自己)
5: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1  注意 一个节点在指向自己的父亲或祖先 都是错误的 即 9-->1 错
6: 1 2 2 1 0 0 也是错误的
这样基本上就可以在poj上通过了,poj(1038)题

这是hdu上通过的代码:
View Code

中间只是稍作修改,把if(m!=n&&find_root(n)==find_root(m))改成了if((m!=n&&find_root(n)==find_root(m))||m==n)就在poj上通过了

 

View Code

 

 

 

 

 '





本文转自NewPanderKing51CTO博客,原文链接:http://www.cnblogs.com/newpanderking/archive/2012/10/14/2723260.html ,如需转载请自行联系原作者


相关文章
关于如何选择正确的U-boot版本
关于如何选择正确的U-boot版本
468 0
|
SQL 运维 监控
高并发接口超时时间过长,导致服务雪崩
高频访问接口超时时间过长,导致服务雪崩
807 0
高并发接口超时时间过长,导致服务雪崩
|
应用服务中间件 Apache
Apache Zookeeper 下载和安装
Apache ZooKeeper 是一个开发和维护开源服务器的项目,它支持高度可靠的分布式协调。 下载地址 北京理工大学 开源软件镜像服务 https://mirror.bit.edu.cn/web/ 清华大学开源软件镜像站 | Tsinghua Open Source Mirror https://mirrors.tuna.tsinghua.edu.cn/ 北京外国语大学开源软件镜像站 | BFSU Open Source Mirror https://mirrors.bfsu.edu.cn/ zookeeper-3.4.14 下载地址 https://mirrors.bfsu.edu.cn
1814 0
Apache Zookeeper 下载和安装
|
安全 Java Spring
Spring Boot 过滤器(Filter)详解
本文详解Spring Boot中过滤器的原理与实践,涵盖Filter接口、执行流程、@Component与FilterRegistrationBean两种实现方式、执行顺序控制及典型应用场景如日志记录、权限验证。对比拦截器,突出其在Servlet容器层的通用性与灵活性,助力构建高效稳定的Web应用。
931 0
|
7月前
|
机器学习/深度学习 人工智能 算法
人工智能技能:未来职场竞争力的核心密码
当机器能理解语言并生成内容,人工智能技能已成为职场必备“新基础能力”。它从技术硬实力扩展为包含技术理解力、人机协作力与伦理判断力的复合能力。未来职场竞争力将取决于人与AI协同创新的深度。通过模块化学习和场景化实践获取这些技能,不同职业阶段需聚焦相应能力发展。掌握AI技能不仅是适应变革,更是拓展职业生命的宽度与深度,开启创造与创新的新篇章。
|
7月前
|
存储 编解码 算法
《从像素到身份:Flutter如何打通社交应用人脸识别的技术闭环》
人脸识别登录是安全便捷的新型登录方式,在Flutter框架下实现需调用原生相机与算法库。其技术原理涵盖人脸检测、图像预处理、特征提取及识别匹配等环节。通过camera库获取人脸图像,借助OpenCV或Dlib等算法库完成识别。为优化体验,需关注性能(如图像压缩、缓存)、交互设计(操作指引、实时反馈)及安全性(数据加密、权限管理)。这一过程挑战与机遇并存,为用户带来全新登录体验。
167 10
|
10月前
|
机器学习/深度学习 人工智能 自然语言处理
快来零门槛、即刻拥有 DeepSeek-R1 满血版
随着人工智能技术的发展,DeepSeek作为一款新兴推理模型,凭借强大的技术实力和广泛的应用场景崭露头角。本文基于阿里云提供的零门槛解决方案,评测DeepSeek的部署与使用。该方案支持多模态任务,涵盖文本生成、代码补全等,融合NLP、IR和ML技术,提供快速实现AI应用的便利。用户无需编码,最快5分钟、最低0元即可部署DeepSeek模型。阿里云还提供100万免费Token,适合预算有限的个人或小型团队试用。通过Chatbox客户端配置API,用户可轻松体验智能交互功能,如数学提问和代码书写等。
46082 7
|
存储 监控 数据安全/隐私保护
GlusterFS存储卷创建
GlusterFS存储卷创建
276 7
|
自然语言处理 索引
RAG入门:理解检索增强生成模型的基本原理
【10月更文挑战第21天】作为一名长期从事自然语言处理(NLP)研究的技术人员,我一直在关注各种新兴技术的发展趋势。其中,检索增强生成(Retrieval-Augmented Generation, RAG)模型引起了我的特别兴趣。RAG技术结合了检索系统和生成模型的优点,旨在解决传统生成模型在处理长文本理解和生成时所面临的挑战。本文将从个人的角度出发,介绍RAG的基本概念、工作原理及其相对于传统生成模型的优势,并探讨一些基本的实现方法。
878 1
|
存储 传感器 物联网