题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29221    Accepted Submission(s): 6719

Problem 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
 
题目大意:就是问你这个是否是一棵树。  树:只有一个根,除跟外每个节点都只有一个入度,也可以为空树
个人思路:自己本来也写了另外一个代码,然而超时了,过程中碰到了output limit error ,发现是数组开小了,还有memory limit error ,可能是杭电判题太严,好像非要用EOF.....。正确做法是并查集来做,将有边的都归为一个集合,当两者已经属于一个集合了,然后又有一条边时,就构成环了,,,再根据边数等于点数-1,得出答案
看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
int node[maxn];
bool vis[maxn];//标记该点是否被访问过
int edge,flag,po;//存边数,点数
void init()
{
memset(vis,false,sizeof(vis));
for(int i=;i<maxn;i++)//题目没有说明范围,开一个足够大的数
node[i]=i;
}
int Find(int a)
{
return a==node[a]?a:node[a]=Find(node[a]);
}
void Union(int a,int b)
{
a=Find(a);
b=Find(b);
if(a==b)
{
flag=;
return ;//这里为何直接退出呢,因为如果a,b已经属于一个集合了,再加一条边就构成环了
}
if(!vis[a])
{
vis[a]=true;//这个点没有访问过的话,点数加一
po++;
}
if(!vis[b])
{
vis[b]=true;
po++;
}
edge++;//边数加一
node[b]=a;
}
int main()
{
ios::sync_with_stdio(false);
int n,m,ca=;
while()
{
scanf("%d%d",&n,&m);
if(n==&&m==)
{
printf("Case %d is a tree.\n",ca++);
continue;
}
if(n<&&m<) break;//注意题目说小于0就退出,我一直以为是两者都等于-1才退出,卡了两个多小时
init();
edge=flag=po=;
Union(n,m);
// while(cin>>n>>m)
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
if(n==m||node[m]!=m) flag=;//两者相等的话也不行,并且只能有一个入度
Union(n,m);
}
if(flag)
{
printf("Case %d is not a tree.\n",ca++);
continue;
}
// for(int i=1;i<=maxn;i++)
// {
// if(vis[i]) po++;
// }
if(edge==po-)//树的点数等于边数加一
printf("Case %d is a tree.\n",ca++);
else
printf("Case %d is not a tree.\n",ca++);
}
return ;
}

Is It A Tree?(hdu1325)的更多相关文章

  1. Is It A Tree?[HDU1325][PKU1308]

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. 【HDU1325】Is It A Tree?(并查集基础题)

    有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...

  3. hdu1325 Is It A Tree?并检查集合

    pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...

  4. HDU1325 Is It A Tree? 【并查集】

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  6. POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28838   Accepted: 9843 -& ...

  7. hdu1325 Is It A Tree?(二叉树的推断)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  9. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

随机推荐

  1. 安装phpredis

    1.下载安装包 https://github.com/nicolasff/phpredis/archive/2.2.5.tar.gz 2.解压到~目录 tar -xvf phpredis-2.2.5. ...

  2. 如何将OpenTSDB源码导入eclipse

    OpenTSDB的官网上介绍了如何将OpenTSDB源码导入eclipse,官方链接,但步骤超级繁琐,还有一个简便方法,下面以导入OpenTSDB2.0.0为例. 1. 下载OpenTSDB2.0.0 ...

  3. JVM类加载(4)—加载器

    定义: 虚拟机设计团队把类加载阶段中“通过一个类的全限定名来获取描述此类的二进制字节流”这个动作放到虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类.实现这个动作的代码模块称之为“类加载器 ...

  4. 谷歌浏览器的input自动填充出现黄色背景解决方案(在已经输入内容之后)

    当你之前提交过表单,再次获取input焦点时,会有一个记录之前填写过的文本的下拉列表式的自动填充效果且带有黄色背景, 这个填充功能本身是没什么问题的,但是谷歌浏览器给了个莫名其妙的黄色背景,用css样 ...

  5. 问题:webservice浏览后 无法输入参数;结果:调试Web Service时不能输入参数的解决办法

    使用.NET 开发Web Service,有一个很方便的功能就是可以通过IE直接测试Web Service.当你的Web Service的参数都是元数据类型,那么只要你使用IE浏览Web Servic ...

  6. [dp]最长单调递增子序列LIS

    https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...

  7. Material使用07 MdGridListModule的使用

    1 MatGridListModule简介 对相似数据的展现,尤其是像是图片的展示 使用起来很像表格 官方文档:点击前往 2 MatGridListModule提供的指令 2.1 mat-grid-l ...

  8. Linux 静态库(.a)转换为动态库(.so)

    Linux 静态库转换为动态库 参考 http://blog.csdn.net/moxuansheng/article/details/5812410 首先将.a文件转为.so文件是可以实现的 原因是 ...

  9. 拦截导弹 (NYOJ—79) 最长字串问题 (NYOJ—17)

    这是到动态规划的题目,属于有顺序的0 1 背包问题: 代码: #include<stdio.h> #include<string.h> ][]; //d[i][j] ]; in ...

  10. jQuery插件AjaxFileUpload可以实现ajax文件上传

    http://blog.sina.com.cn/s/blog_55e42da60100ocvh.html