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): 1145 Accepted Submission(s): 355 |
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 |
Sample Output
Case 1 is a tree. |
Source
North Central North America 1997
|
Recommend
Ignatius.L
|
/*
不能有环,每个点的入度小于2
*/
#include<bits/stdc++.h>
using namespace std;
int bin[];
int vis[];
int findx(int x)
{
while(bin[x]!=x)
x=bin[x];
return x;
}
int build(int a,int b)
{
int fx=findx(a);
int fy=findx(b);
if(fx!=fy)
{
bin[fx]=fy;
return false;
}
else//有环
return true;
}
int cur=;
int f=;
int len=;
vector<int >v;
int a,b;
map<int ,int>m;
void inti()
{
for(int i=;i<;i++)
{
f=;
cur=;
len=;
bin[i]=i;
vis[i]=false;
}
v.clear();
m.clear();
}
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
int c=;
while(scanf("%d%d",&a,&b)!=EOF)//输入的是每组的第一组数
{
if(a<&&b<)
break;
if(!a&&!b)
{
printf("Case %d is a tree.\n",c++);
continue;
}
inti();
if(!vis[a])
v.push_back(a);
if(!vis[b])
v.push_back(b);
vis[a]=true;
vis[b]=true;
if(build(a,b))
f=;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(!a&&!b)
break;
if(!vis[a])
v.push_back(a);
if(!vis[b])
v.push_back(b);
vis[a]=true;
vis[b]=true;
if(m[b]==)
m[b]++;
else
f=;
if(build(a,b))
f=;
}
if(!f)
{
int root=findx(v[]);
for(int i=;i<v.size();i++)
{
if(findx(v[i])!=root)
{
f=;
break;
}
}
}
if(!f)
printf("Case %d is a tree.\n",c++);
else
printf("Case %d is not a tree.\n",c++); }
return ;
}
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 ...
随机推荐
- angularjs之ui-bootstrap的Datepicker Popup实现双日期选择控件
最开始使用ui-bootstrap的Datepicker Popup日期选择插件实现双日期选择时间范围时,在网上搜了一些通过JS去实现的方法,不过后来发现可以不必通过JS去处理,只需要使用其自身的属性 ...
- JS -- The Scope Chain 作用域链
The Scope Chain JavaScript is a lexically scoped language: the scope of a variable can be thought of ...
- 如何写一个jquery插件
本文总结整理一下如何写一个jquery插件?虽然现今各种mvvm框架异常火爆,但是jquery这个陪伴我们成长,给我们带来很多帮助的优秀的库不应该被我们抛弃,写此文章,作为对以往欠下的笔记的补充, ...
- 【转】elasticsearch的查询器query与过滤器filter的区别
很多刚学elasticsearch的人对于查询方面很是苦恼,说实话es的查询语法真心不简单- 当然你如果入门之后,会发现elasticsearch的rest api设计是多么有意思. 说正题,ela ...
- “==”与"equals(object)"的区别
一.对于基本数据类型而言只能用“==”,不能用equals来进行比较,若使用equals来进行比较,则不能通过编译 二.在非字符串的对象的比较中: “==”与“equals()”比较的均是对象在堆内存 ...
- java异步线程池同时请求多个接口数据
一.主要使用类 . ExecutorService java线程池类 申明方式:ExecutorService exc = Executors.newFixedThreadPool(requestPa ...
- 跨平台移动端APP开发---简单高效的MUI框架
MUI是dcloud(数字天堂)公司旗下的一款跨平台开发移动APP的框架产品,在学习MUI框架之前,最先接触了Hbuilder代码编辑器,它带给我的第一感觉是快,这是HBuilder的最大优势,通过完 ...
- Docker入门之七Dockerfile
Dockerfile是一个文本格式的配置文本,可以使用它来创建自定义的镜像.首先我们可以先看一个dockerfile是什么样子.这里可以有一个网站不错:http://dockerfile.github ...
- Myeclipse 配置Tomcat 出现 “Value must be an existing directory”错误
今天上午配了一下本机上的Myeclipse的tomcat,因为我本机上有两个版本的myeclipse,一个是用来公司开发的,一个是自己玩的,本机上装了两个版本jdk和两个版本的tomcat.配置自己玩 ...
- c# Linq操作XML,查找节点数据
/*查找XML*/ var filePath = Server.MapPath("~/xml/sample.xml"); XDocument doc = XDocument.Loa ...