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 ...
随机推荐
- 深入理解计算机系统chapter8
进程轮流使用处理器 父进程调用fork来创建一个新的子进程 回收子进程 waitpid/wait 非本地跳转:
- 使用jquery的方法和技巧2,点击多选框的jquery响应
使用jquery来控制多选框的变化 功能描述: 1.第一层 当选中后台应用(App1)时,所有多选框都被选择. 当取消选中后台应用(App1)时,所有多选框都被取消选择. 第一层的逻辑如下: 2.第二 ...
- spring 内部工作机制(二)
本章节讲Spring容器从加载配置文件到创建出一个完整Bean的作业流程及参与的角色. Spring 启动时读取应用程序提供的Bean配置信息,并在Spring容器中生成一份相应的Bean配置注册表, ...
- 简单Elixir游戏服务器-安装Elixir
用WebInstaller 安装半天也没下载成功文件. 改成直接下载erlang 和 elixir 预编译包了. 安装很简单,最后设置好环境变量. cmd 执行 elixir -v 最后顺便下载了个g ...
- Promise 对象
Promise 对象用于处理异步请求,保存一个异步操作最终完成(或失败)的结果 语法 new Promise( /* executor */ function(resolve, reject) {.. ...
- WPF之DataGrid应用
前几天打算尝试下DataGrid的用法,起初以为应该很简单,可后来被各种使用方法和功能实现所折磨.网络上的解决方法太多,但也太杂.没法子,我只好硬着头皮阅览各种文献资料,然后不断的去尝试,总算小有成果 ...
- 开源API集成测试工具 Hitchhiker v0.2更新 - 压力测试
Hitchhiker 是一款开源的 Restful Api 集成测试工具,支持Schedule, 数据对比,压力测试,可以轻松部署到本地,和你的team成员一起管理Api. 详细介绍请看: http: ...
- npm install 时--save-dev和--save的区别
package.json中两个字段含义简介 一直在使用npm包管理器,对于npm install module --save-dev 和 npm install module --save这两个的区别 ...
- 当 ReactJS 遇到 TypeScript
由于本人应用项目主要是2B的web应用程序, 所以以下暂不谈react的服务端渲染和react navie, 先说说ReactJS: React 的组件化和 数据单向绑定 的模式(flux) 使应用 ...
- Hadoop(九)Hadoop IO之Compression和Codecs
前言 前面一篇介绍了Java怎么去查看数据块的相关信息和怎么去查看文件系统.我们只要知道怎么去查看就行了!接下来我分享的是Hadoop的I/O操作. 在Hadoop中为什么要去使用压缩(Compres ...