B - Is It A Tree?
来源 hdu 1325
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<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
int pre[100005],temp,visit[100005],num[100005];//num是保存被指向的数目
int find(int x)
{
if(pre[x]==x) return x;
return pre[x]=find(pre[x]);
}
void Union(int x,int y)
{
int a=find(x),b=find(y);
if(a==b)
{
temp=1;
return ;
}
pre[b]=a;
}
int main()
{
int x,y,ans=1;
while(1)
{
temp=0;
mm(visit,0);
mm(num,0);
int sum=0;
rep(i,0,100005) pre[i]=i;
while(1)
{
sf("%d%d",&x,&y);
if(x+y==0) break;
if(x+y<0) return 0;
Union(x,y);
visit[x]=1;visit[y]=1;
num[y]++;
}
rep(i,0,100005)
{
if(visit[i]&&pre[i]==i) sum++;//判断是否是森林
if(num[i]>1) temp=1;被指向超过两次
}
if(temp||sum>1)
pf("Case %d is not a tree.\n",ans++);
else
pf("Case %d is a tree.\n",ans++);
}
}
B - Is It A Tree?的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Tree树节点选中及取消和指定节点的隐藏
指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...
随机推荐
- 奇怪吸引子---Hadley
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- 如何Python下载大文件?
我想用python脚本下载很多文件,但是经常就有那么几个出错,写了个error handling,跳了过去,但是把出错的链接保存了一下. 转过天来,研究了一下出的什么错. 一个报错如下: PS C:\ ...
- oracle 常用 sql
判断字段值是否为空( mysql 为 ifnull(,)): nvl (Withinfocode,'') as *** 两字段拼接: (1)concat(t.indate, t.intime) as ...
- 在Windows上使用CMake+CMakeLists.txt生成VS项目编译DlibDotnet库
一.下次源库 地址:https://github.com/takuya-takeuchi/DlibDotNet 二.下载CMake3以上版本 地址:https://cmake.org/download ...
- CSS中的偏僻知识点
一.css中的calc 在CSS中有calc属性用于尺寸的计算,可以让百分比和像素值进行运算. div {width : calc(100% - 30px);} 为了兼容性 /*Firefox*/ - ...
- LaTeX 中使两张表格并排
在使用 LaTeX写论文或者画海报的时候,希望两张较小的表格可以并排,(一般情况的LaTeX插入两张图片是上下布局的) 查找了一下,相关的例子如下: \begin{minipage}{\textwid ...
- 整死你个妖精,CDN西游捉妖记!
CDN的降价潮和撕逼季已过,终于轮到小黑羊来做个科普啦. 这事儿,要从西游记取经开始…… [本图来自肖传湛个人网站:www.moko.cc/hiyoko] 1300年前,唐僧师徒取经要跋涉十万八千里, ...
- 远程桌面中Tab键不能补全的解决办法
我们曾在之前的一篇文章中介绍了windows远程连接ubuntu的方法,在成功登陆远程桌面环境之后,发现在终端中Tab键不能自动补齐(但是Ctrl +Tab 可以用,但是需要按下组合键才能补全的话,时 ...
- 每日英语:The Right Way to Network
With startup-themed conferences, hackathons, meet-ups and cocktail hours regularly taking place, ent ...
- iOS应用开发,全局强制竖屏,部分页面允许旋转的处理
目前大多数app都不支持横屏模式,用户不常用,开发起来也麻烦一些.但有些时候,又离不开横屏和竖屏的切换,比如查看视频.图片.文档等,这时又不得不对页面做横屏的处理.下面来教大家如何处理这种场景,方法可 ...