博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1308 Is It A Tree?
阅读量:2442 次
发布时间:2019-05-10

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

Is It A Tree?
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22790   Accepted: 7814

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 45 6  0 08 1  7 3  6 2  8 9  7 57 4  7 8  7 6  0 03 8  6 8  6 45 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.

题意:判断由这些组成的树是不是一棵树;

解题:并查集的应用;

注意:

1.空树也是树;

2.不能有环;

3.不能是森林;

参考代码:

#include 
#include
#include
using namespace std;int father[30000+1],rank[30000+1];bool flag[30000+1];void init(int n){ for (int i=0;i<=n;i++){ father[i]=i; rank[i]=1; flag[i]=false; }}int find(int x){ if (x!=father[x]) father[x] = find(father[x]); //路径压缩 return father[x];}void Union(int x,int y){ int i=find(x); int j=find(y); if (i==j) return; if (rank[i]
>a>>b){ if (a==b&&a==-1) break; if (a==0&&b==0){ cout<<"Case "<
<<" is a tree."<
>a>>b){ if (a==0&&b==0) break; if (find(a)==find(b)) tree=false; flag[a]=flag[b]=true; Union(a,b); } for (int i=1;i<=1000;i++){ if (flag[i]==true&&find(i)!=find(first)){ tree=false; break; } } if (tree==false) cout<<"Case "<
<<" is not a tree."<

转载地址:http://gybqb.baihongyu.com/

你可能感兴趣的文章
中文版Windows XP 的新增功能(转)
查看>>
Web Application 開 發 利 器 - WebSnap(三) (转)
查看>>
跟我学 安装Windows Vista Bata2实录(转)
查看>>
Windows Vista IIS 7.0开启方法(转)
查看>>
Windows Vista六大版本详细介绍(转)
查看>>
Windows XP 中注册表内容的导入和导出(转)
查看>>
Linux日志式文件系统面面观(转)
查看>>
正版风暴让盖茨命不盖绝 Linux祸福难料(转)
查看>>
单一产品不会成功 开源软件开始商业应用(转)
查看>>
RedHat上SSH2的安装和使用(转)
查看>>
安全使用RedHat Linux系统(转)
查看>>
RedHat Enterprise AS4硬盘安装步骤(转)
查看>>
全国第一个高校Linux培训考试中心建立(转)
查看>>
Linux黑客大曝光:Linux安全机密与解决方案(转)
查看>>
Gentoo Linux CD 方式全程安装过程(转)
查看>>
关于Kerberos安装的几个问题(转)
查看>>
Solaris硬盘分区简介(转)
查看>>
gcc编译器小知识FAQ(转)
查看>>
Linux下多线程编程与信号处理易疏忽的一个例子(转)
查看>>
流氓和木马结合 强行关闭你的防火墙(转)
查看>>