ACM1325Is it A tree?
通过这道简单而又坑人的题目,练习并查集和set 容器的使用;
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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.
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
Case 2 is a tree.
Case 3 is not a tree.
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
const int N=;
int p[N];
bool flag;
int find(int x)
{
if(x==p[x])
return x;
return p[x]=find(p[x]);
}
void link(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa==fb)
flag=true;
else
{
if(fb!=b)
flag=true;
p[fb]=fa;
}
}
int main()
{
int n,m;
int cd=;
while(true)
{
set<int>q;
flag=false;
for(int i=;i<N;i++)
p[i]=i;
scanf("%d %d",&n,&m);
if(n==&&m==)
{
printf("Case %d is a tree.\n",cd);
cd++;
continue;
}
if(n<&&m<)break;
q.insert(n);
q.insert(m);
link(n,m);
while(scanf("%d%d",&n,&m)==&&n&&m)
{
q.insert(n);
q.insert(m);
link(n,m);
}
int count=-;
set<int>::iterator it;
for(it=q.begin();it!=q.end();it++)
{
if(p[*it]==*it)
count++;
}
if(count>)flag=true;
if(flag==true)
printf("Case %d is not a tree.\n",cd);
else printf("Case %d is a tree.\n",cd);
cd++;
}
return ;
}
ACM1325Is 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 ...
 
随机推荐
- tomcat下载、安装
			
下载 官网地址:https://tomcat.apache.org/download-80.cgi 安装 直接安装即可.安装完毕后Tomcat的目录结构如下: bin:脚本目录  启动脚本:star ...
 - phantomjs抛出IOException
			
使用phantomjs对网页进行截图遇到的问题 问题描述: 使用的phantomjs版本:phantomjs-2.1.1-windows 使用的截图js文件,\phantomjs-2.1.1-wind ...
 - Python基础简介
			
一.目前各种语言的应用:java, 可以把特别小的项目做大,并且开源库比较多,C: 用在最底层,例如编写操作系统,运行速率快,开发效率低,C++:常坐游戏引擎Python:AI(人工智能) 简单.明确 ...
 - 隐马尔科夫模型(hidden Markov Model)
			
万事开头难啊,刚开头确实不知道该怎么写才能比较有水平,这篇博客可能会比较长,隐马尔科夫模型将会从以下几个方面进行叙述:1 隐马尔科夫模型的概率计算法 2 隐马尔科夫模型的学习算法 3 隐马尔科夫模型 ...
 - C语言struct中的长度可变数组(Flexible array member)
			
C_struct中的长度可变数组(Flexible array member) Flexible array member is a feature introduced in the C99 sta ...
 - Mininet实验 MAC地址学习分析
			
拓扑图 学习过程分析 首先交换机A和交换机B一开始的MAC地址表都是空的. 此时主机11向主机33发送一个数据帧. 数据帧会先到达交换机A,交换机A会获得主机11的MAC地址和端口号.(此时交换机A的 ...
 - iOS开发UUIView动画方法总结
			
#动画设置 UIView动画实现 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *myView; @ ...
 - ubuntu软件管理apt与dpkg
			
目前ubuntu系统主要有dpkg和apt两种软件管理方式两种区别如下 1.dpkg是用来安装.deb文件,但不会解决模块的依赖关系,且不会关心ubuntu的软件仓库内的软件,可以用于安装本地的deb ...
 - 使用LoadRunner脚本采集Linux性能数据
			
前面介绍过在LoadRunner的Java协议实现“使用SSH连接Linux”.下面的脚本,是在LoadRunner里连接Linux/Unix远程服务器,收集其磁盘IO的负载到测试结果. 涉及到三个知 ...
 - Bookmark Sentry – 检查重复、删除死链书签 Chrome扩展
			
Bookmark Sentry 的用处,就是 处理重复的收藏夹的死链 . 重复链收藏.具体,请百度. Bookmark Sentry 下载 : https://files.cnblogs.com/f ...