题目链接: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. from selenium.webdriver.support.ui import Select

    from selenium.webdriver.support.ui import Select Select(d.find_element_by_id(u'key_开户行')).first_sele ...

  2. java对象在内存中的结构(HotSpot虚拟机)

    一.对象的内存布局 HotSpot虚拟机中,对象在内存中存储的布局可以分为三块区域:对象头(Header).实例数据(Instance Data)和对齐填充(Padding). 从上面的这张图里面可以 ...

  3. 给JZ2440开发板重新分区

    转自:http://mp.weixin.qq.com/s?__biz=MzAxNTAyOTczMw==&mid=2649328035&idx=1&sn=7d3935cc05d3 ...

  4. vs快捷键复制当前行

    vs快捷键 1)如果你想复制一整行代码,只需将光标移至该行,再使用组合键“Ctrl+C”来完成复制操作,而无需选择整行.2)如果你想剪切一整行代码,只需将光标移至该行,再使用组合键“Ctrl+X”来完 ...

  5. Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类

    1.转自:https://blog.csdn.net/u013802160/article/details/51815077 <?xml version="1.0" enco ...

  6. navicat自动备份

    http://blog.csdn.net/eastmount/article/details/70239244

  7. 利用httpclient和mysql模拟搜索引擎

    数据抓取模块 package crowling1; import java.sql.CallableStatement; import java.sql.Connection; import java ...

  8. [51nod1043]幸运号码

    题意:1个长度为2N的数,如果左边N个数的和 = 右边N个数的和,那么就是一个幸运号码. 例如:99.1230.123312是幸运号码. 给出一个N,求长度为2N的幸运号码的数量.由于数量很大,输出数 ...

  9. 6、git和github

    参考:http://www.cnblogs.com/qianmojing/p/6484162.htmlhttp://www.jb51.net/article/70729.htmhttp://www.c ...

  10. jquery 规范

    使用单引号 不推荐 $("div").html("<img src='1.jpg'>"); 推荐 $('div').html('<img sr ...