Is It A Tree? (并查集)(C++中set的运用)
Is It A Tree?
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即可
学会用set了好开森 ^ _ ^
AC代码:
#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
int a,b;
int cas=1;
while(~scanf("%d%d",&a,&b) && (a!=-1 && b!=-1))
{
if(a==0 && b==0)
{
printf("Case %d is a tree.\n",cas++);
continue;
}
set<int>s;
s.insert(a);
s.insert(b);
int m=1;
while(scanf("%d%d",&a,&b) && (a || b))
{
s.insert(a);
s.insert(b);//把这两个点放入集合
m++;//要统计边的条数
}
if(s.size()-m==1)//如果满足点的数量减边的条数为1,那么说明不存在环
{
printf("Case %d is a tree.\n",cas++);
}
else//否则存在环,即不是树
{
printf("Case %d is not a tree.\n",cas++);
}
}
return 0;
}
- 性能分析工具-PerfView
- c#测试字符串是否为GUID的几种方法
- 编译错误CS1595
- 分享一款带尖角浮出 公告栏 样式
- 在asp.net页面上得到Castle容器的实例
- WordPress自带TinyMCE编辑器相关功能增强
- 联众互动收购3家棋牌游戏公司,其域名买自蔡文胜手中
- 未来有什么工作绝对不会被人工智能取代
- WordPress在RSS Feed 中输出版权信息
- WordPress免插件仅代码实现文章浏览次数的方法(2)
- 苹果被告了,但网友们却觉得一点也不亏!
- Castle 整合.NET Remoting
- 这三要素,让区块链技术成为颠覆世界的技术
- 代码实现 WordPress 反垃圾评论功能
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- AngularJS 中的 controllerAs
- Android-Jetpack笔记-DataBinding
- Android-Jetpack笔记-Lifecycles
- Android-Jetpack笔记-LiveData
- Android-Jetpack笔记-ViewModelSavedState
- Android-Jetpack笔记-ViewModel
- GitLab CE 安装记录
- SparkSQL中产生笛卡尔积的几种典型场景以及处理策略
- TypeScript 中的 export 和 import
- Angular2 初体验
- 在 Ubuntu 系统上配置 Nginx Git 服务器
- 设计模式之规格模式
- Android-Jetpack笔记-Navigation之Fragment使用
- Android-Jetpack笔记-Navigation之Fragment支持复用
- Android-Jetpack笔记-Room