TOJ 1856 Is It A Tree?
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 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.
Source
North Central North America 1997
只要判断必须有一个结点的入度为0,所有结点的入度不能大于1。
如果没有结点也是成立的。(坑)
#include <stdio.h>
#include <string.h>
#include <set>
#define MAXN 100010
using namespace std; int indegree[MAXN];
set<int> S;
set<int>::iterator it; bool judege(){
int flag=;
for(it=S.begin(); it!=S.end(); it++){
if(indegree[*it]==)flag++;
if(indegree[*it]>)return false;
}
if(flag==)return true;
else return false;
} int main()
{
int c=;
int u,v;
while( scanf("%d %d" ,&u ,&v)!=EOF ){
if(u==- && v==-)break;
if(u== && v==){
if(judege() || S.size()==){
printf("Case %d is a tree.\n",++c);
}else{
printf("Case %d is not a tree.\n",++c);
}
memset(indegree , ,sizeof(indegree));
S.clear();
}else{
indegree[v]++;
S.insert(u);
S.insert(v);
}
}
return ;
}
TOJ 1856 Is It A Tree?的更多相关文章
- 最小生成树 TOJ 4117 Happy tree friends
链接http://acm.tju.edu.cn/toj/showp4117.html 4117. Happy tree friends Time Limit: 1.0 Seconds Memo ...
- TOJ 4008 The Leaf Eaters(容斥定理)
Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eat ...
- hdu1325 Is It A Tree?(二叉树的推断)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- [数据结构]——二叉树(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 ...
随机推荐
- mvc+EF - 有用文章
Mvc全局过滤器与Action排除:http://blog.csdn.net/shuaihj/article/details/53020428 MVC 自定义AuthorizeAttribute实现权 ...
- 「BZOJ 2142」礼物
题目链接 戳这 Title Solution 这一道题显然可以看出公式为: \[ans=C_{n}^{w_1}*C_{n-w}^{w_2}*...*C_{w_m}^{w_m}\] 然后就可以用扩展Lu ...
- ubuntu14.04,安装Chrome(谷歌浏览器)
Linux:ubuntu14.04 一直都很喜欢谷歌浏览器,进入linux怎么能没有? 安装方法:谷歌浏览器官方下载的ubuntu版本,下载后点击即可安装. 下载地址:http://download. ...
- Kafka,Mq,Redis作为消息队列有何差异?
Kafka作为新一代的消息系统,mq是比较成熟消息系统,而redis也可以发布订阅,那么这三者有何异同? RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,X ...
- 3. C/C++笔试面试经典题目三
64. 如何打印出当前源文件的文件名以及源文件的当前行号? [参考答案]cout << __FILE__ ; cout<<__LINE__ ; __FILE__和__LINE_ ...
- 1. UML统一建模语言
(1)UML概述: 建模: 对现实系统进行适当的过滤, 用适当的表现规则描述出简洁的模型. 建模是一种深入解决问题的方法. UML: UML(United Modeling Language, 统一建 ...
- [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法
最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...
- tomcat更改web文件路径
由于代码太长,记不住!只能自己做个小笔记了!! <Context path="/" docBase="/opt/appl/merch.bak" debug ...
- 【转】idea project中导入其他文件夹下的模块,可能出现java.io.FileNotFoundException: XXX.xml
在一个project 中导入一个java 模块, 我要执行该模块的main函数 ,在main函数中有一个 FileReader(“generatorConfig.xml”) 而generatorCon ...
- java 常用模块代码
1.文件的读写操作 (1)进行读操作 import java.io.BufferedReader; import java.io.FileNotFoundException; import java. ...