博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1308 Is It A Tree? (并查集)
阅读量:4307 次
发布时间:2019-06-06

本文共 2207 字,大约阅读时间需要 7 分钟。

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.
764119-20160727103014638-1558301427.png

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.

题意:

判断给出的图是否是一棵树.

题解:

这题跟HDU1272判断联通无环图一模一样,图的有向无向并不造成影响.
().

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long#define eps 1e-8#define maxn 101000#define mod 100000007#define inf 0x3f3f3f3f#define IN freopen("in.txt","r",stdin);using namespace std;int fa[maxn];int _rank[maxn];bool vis[maxn];void init_set() { for(int i=0; i

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5709548.html

你可能感兴趣的文章
FFmpeg 源码分析 - avcodec_send_packet 和 avcodec_receive_frame
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>
MongoDB基本语法和操作入门
查看>>
学习笔记_vnpy实战培训day04_作业
查看>>
OCO订单(委托)
查看>>
学习笔记_vnpy实战培训day06
查看>>
回测引擎代码分析流程图
查看>>
Excel 如何制作时间轴
查看>>
股票网格交易策略
查看>>
matplotlib绘图跳过时间段的处理方案
查看>>
vnpy学习_04回测评价指标的缺陷
查看>>
ubuntu终端一次多条命令方法和区别
查看>>