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 ...
随机推荐
- java基础——java.util.ConcurrentModificationException
在编写代码的时候,有时候会遇到List里有符合条件的的对象,就移除改对象! 但是这种操作如:使用了 List 的remove,会导致一些很严重的问题! 如下这段代码使用ArrayList: @Test ...
- DLL生成与使用的全过程
由dll导出的lib文件: 包含了每一个dll导出函数的符号名和可选择的标识号以及dll文件名,不含有实际的代码(这里的lib文件和静态库是不一样的),其中的导出导入函数都 是跳转指令,直接跳转到DL ...
- Find 找规律,递推
Find Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus P ...
- java数据库编程之事务、视图、索引、备份、恢复
第五章:事务.视图.索引.备份和恢复 5.1:事务 事务的概念:事务(transcation)是讲一系列数据操作捆绑成为一个整体进行统计管理. 如果某一事务执行成功了,则该事务进行操作的所有数据将会提 ...
- python使用装饰器@函数式化django开发
django是一个python web开发的框架.作为一个框架MVC的架构已经实现起来了.但是编码的时候你经常要进行进一步的抽象. AOP是一种称为面向切面的开发思想,意思是将部分功能代码在运行时动态 ...
- Hadoop通过HCatalog编写Mapreduce任务访问hive库中schema数据
1.dirver package com.kangaroo.hadoop.drive; import java.util.Map; import java.util.Properties; impor ...
- java虚拟机指令dup的理解
举个例子: public class ExceptionTest{ void cantBeZero(int i) throws Exception{ throw new Exception(); } ...
- .NET Framework基本概念
http://blog.csdn.net/T573029173/article/details/41730101 .NET是微软的新一代技术平台.对技术人员来说,想真正了解什么是.NET,须先了解.N ...
- 深入浅出:JavaScript作用域链
1. 什么是作用域 任何程序设计语言都有作用域的概念,简单的说,作用域就是变量的作用范围. 2. 变量的分类和变量作用域的分类 在JavaScript中,变量分为全局变量和局部变量,与此相对应的,变量 ...
- win10 & Ubuntu16 双系统安装
忽然心血来潮吧,本机在已经安装了win10的背景下,想要再加一个linux系统学习学习,几经波折,终于成功. 博主笔记本里有两块固态,一个250G的装了win10,装的时间不久,镜像是在msdn上下载 ...


