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 ...
随机推荐
- 语音传输之RTP/RTCP/UDP及软件实现关键点
语音通信是实时通信,一定要保证实时性,不然用户体验会很糟糕.IETF设计了RTP来承载语音等实时性要求很高的数据,同时设计了RTCP来保证服务质量(RTP不保证服务质量).在传输层,一般选用UDP而不 ...
- Spring+SpringMVC+MyBatis整合进阶篇(四)RESTful实战(前端代码修改)
前言 前文<RESTful API实战笔记(接口设计及Java后端实现)>中介绍了RESTful中后端开发的实现,主要是接口地址修改和返回数据的格式及规范的修改,本文则简单介绍一下,RES ...
- JAVA多线程---volatile关键字
加锁机制既可以确保可见性又可以保证原子性,而volatile变量只能确保可见性. 当把变量声明为volatile时候 编译器与运行时都会注意到这个变量是共享的,不会将该变量上的操作与其他内存操作一起重 ...
- 在分布式数据库中CAP原理CAP+BASE
本篇博文的内容均来源于网络,本人只是整理,仅供学习! 一.关系型数据库 关系型数据库遵循ACID规则 事务在英文中是transaction,和现实世界中的交易很类似,它有如下四个特性: 1.A (At ...
- 【JVM命令系列】jstat
命令基本概述 Jstat是JDK自带的一个轻量级小工具.全称"Java Virtual Machine statistics monitoring tool",它位于java的bi ...
- 关于select的一个错误---属性选择器
错误: jquery 获取下拉框 text='1'的 option 的value 属性值 我写的var t= $("#selectID option[text='1']).val() ; ...
- Codeforces 845 A. Chess Tourney 思路:简单逻辑题
题目: 题意:输入一个整数n,接着输入2*n个数字,代表2*n个选手的实力. 实力值大的选手可以赢实力值小的选手,实力值相同则都有可能赢. 叫你把这2*n个选手分成2个有n个选手的队伍. ...
- Scala 中的隐式转换和隐式参数
隐式定义是指编译器为了修正类型错误而允许插入到程序中的定义. 举例: 正常情况下"120"/12显然会报错,因为 String 类并没有实现 / 这个方法,我们无法去决定 Stri ...
- 跨平台移动端APP开发---简单高效的MUI框架
MUI是dcloud(数字天堂)公司旗下的一款跨平台开发移动APP的框架产品,在学习MUI框架之前,最先接触了Hbuilder代码编辑器,它带给我的第一感觉是快,这是HBuilder的最大优势,通过完 ...
- Python s12 Day3 笔记及作业
1. Set集合 old_dict = { "#1":{ 'hostname':'c1', 'cpu_count':2, 'mem_capicity':16}, "#2& ...