题目链接: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. HDOJ1495(倒水BFS)

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. Excel特殊格式的列

    1.根据前两列显示天时分格式,算出所需时间列的内容=DAY(O2-N2)&"天"&HOUR(O2-N2)&"小时"&MINUTE ...

  3. QT4.8.5+qt-vs-addin-1.1.11+VS2010安装配置和QT工程的新建和加载

    1.下载windows下的QT库 QT4.8.5 for vs2010: http://download.qt-project.org/official_releases/qt/4.8/4.8.5/q ...

  4. 【转】 Pro Android学习笔记(六四):安全和权限(1):签发apk

    目录(?)[-] Android安全模型 数字证书签发 Debug的keystore 生产unsigned的apk 为apk进行证书签发 align安装包 使用Export Wizard生成签发的ap ...

  5. Java数组的基本讲解

    由于Java数组涵盖的内容比较多,这里从个人的角度对主要的内容进行相关的讲解. 如有不足,欢迎批评指正~ 1)Java数组是动态还是静态的啊?     Java语言是典型的静态语言,由此推断Java数 ...

  6. jquery插件开发常用总结一

    由于使用jquery插件后当form表单提交的时候,若发生错误,同时有验证错误文本时,即使用rules和message后,会自动生成一个label标签里面装有错误文件值. 我们可以替换它: 方式为:v ...

  7. springMVC绑定json参数之二(2.2.4)

    二.springmvc 接收不同格式的json字符串 4).格式四:json传递复杂对象(对象中有属性,还有List,这里验证数组接收,不用List,其他的和上一节不变) 测试一: Test对象属性如 ...

  8. Android精品资源汇总,10个源码(持续更新)

    最近一直在学习Android,在各大社区逛,总结下自己看到的一些不错的源码.希望可以给大家带来帮助. 1.Android精品源码:带动态效果的Button(按钮) 最喜欢各种效果的按钮了,没办法就是这 ...

  9. Math类简介

    Math  abs max min 分别是绝对值 最大值,最小值 round 四舍五入 ceil ceil(32.6)  33.0 ceil(32.2) 33.0 返回大于该数值的较大的整数 与之相对 ...

  10. 关于android 数据库查询出现 _id column do not exist 的处理

    查询的字段必须带上_id字段,否则就会出现此类异常,导致程序崩溃