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 ...
随机推荐
- WndProc
主要用在拦截并处理系统消息和自定义消息. form窗体的一个重载方法. protected override void WndProc(ref Message m) { //拦截窗体最小化按钮消息 i ...
- U盘安装Ubuntu 12.04成功后系统无法启动的原因及解决办法
想搭建一个Linux开发环境,选择了ubuntu12.04长期支持版,采用u盘安装(Universal-USB-Installer做的启动),发现安装完成之后,拔掉u盘无法启动,插上u盘之后,可以重启 ...
- window.document 对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- vs 2017局域网内调试
之前调试代码都是在本地启动服务,以 localhost:端口号 的形式调试,今天发现也是可以用ip地址的形式来调用接口,这种方式可以支持内网内Client端调用接口,实现调试的功能,具体方法如下 ...
- [转]解读Unity中的CG编写Shader系列1——初识CG
CG=C for Graphics 用于计算机图形编程的C语言超集 前提知识点: 1.CG代码必须用 CGPROGRAM ... ENDCG括起来 2.顶点着色器与片段着色器的主函数名称可随意,但需 ...
- oracle为IN OUT变量或OUT变量赋值时提示“表达式''不能用作赋值目标”
是因为IN OUT变量和OUT变量是要输出的,不能赋给它常量值,这样它就不能再被赋值而输出了,所以是禁止赋常量值的,比如''也是常量值,也不可以赋给这两种类型的变量,如果不能把存储过程中的其他变量赋给 ...
- Qt 学习之路 2(70):进程间通信
Qt 学习之路 2(70):进程间通信 豆子 2013年11月12日 Qt 学习之路 2 16条评论 上一章我们了解了有关进程的基本知识.我们将进程理解为相互独立的正在运行的程序.由于二者是相互独立的 ...
- java程序员的从0到1:统计某字符串在某文件中出现的次数(面试题)
目录: 1. 编程题目 2. 方法一 3. 方法二 4. 方法三 5. 方法四 6. 总结 正文: 1. 编程题目 写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数. 2. ...
- ES6问题记录--Promise
在读[阮一峰]的 ES6 入门文档的时候,有一段代码是这样的. 文章中,还有一段是这样写的 所以,Promise.resolve(3).finally(()=>{}),解析出来的resolve ...
- 洛谷P2709 小B的询问
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...