题目链接: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
North Central North America 1997
题目大意:就是问你这个是否是一棵树。  树:只有一个根,除跟外每个节点都只有一个入度,也可以为空树
个人思路:自己本来也写了另外一个代码,然而超时了,过程中碰到了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+7;
const int maxn=1e5+1;
const int maxk=100+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
int node[maxn];
bool vis[maxn];//标记该点是否被访问过
int edge,flag,po;//存边数,点数
void init()
{memset(vis,false,sizeof(vis));for(int i=0;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=1;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=1;while(1){scanf("%d%d",&n,&m);if(n==0&&m==0){printf("Case %d is a tree.\n",ca++);continue;}if(n<0&&m<0) break;//注意题目说小于0就退出,我一直以为是两者都等于-1才退出,卡了两个多小时
        init();edge=flag=po=0;Union(n,m);//  while(cin>>n>>m)while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0) break;if(n==m||node[m]!=m) flag=1;//两者相等的话也不行,并且只能有一个入度
            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-1)//树的点数等于边数加一printf("Case %d is a tree.\n",ca++);elseprintf("Case %d is not a tree.\n",ca++);}return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/9392045.html

Is It A Tree?(hdu1325)相关推荐

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

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

  2. UVALive5461 UVA615 POJ1308 Is It A Tree?(解法二)【废除!!!】

    本文废除!!! 参考链接:UVALive5461 UVA615 POJ1308 ZOJ1268 Is It A Tree?[并查集] Regionals 1997 >> North Ame ...

  3. 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  4. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

  5. Python---哈夫曼树---Huffman Tree

    今天要讲的是天才哈夫曼的哈夫曼编码,这是树形数据结构的一个典型应用. !!!敲黑板!!!哈夫曼树的构建以及编码方式将是我们的学习重点. 老方式,代码+解释,手把手教你Python完成哈夫曼编码的全过程 ...

  6. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. Code Forces Bear and Forgotten Tree 3 639B

    B. Bear and Forgotten Tree 3 time limit per test2 seconds memory limit per test256 megabytes inputst ...

  8. Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)

    Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...

  9. datagrid底部显示水平滚动_DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强

    行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...

最新文章

  1. Windows Mobile 6.0 SDK和中文模拟器下载
  2. js php 时间格式化字符串,JS怎么实现字符串与日期的互相转换及日期的格式化
  3. 据库中事务、会话、线程这几个概念是什么关系
  4. VC程序中运行其他程序的三种方法
  5. python创建django项目_搭建Python-Django环境,创建第一个Django项目
  6. leetcode —— 1079. 活字印刷
  7. 为了生产iPhone 12,富士康连国庆节也没假放了
  8. Spark Job Scheduling
  9. bootstrap不同分辨率显示滚动条_bootstrap基础快速入门-8 响应式显示与隐藏
  10. 视频教程-大数据编程语言scala讲座-其他
  11. Rhino基础教程---三管混接(法二、法三)
  12. mac linux 键盘布局,Macbook Pro 推出中文键盘布局
  13. 优酷屏幕录制在哪里_手机优酷怎么录制视频
  14. 计算机专业英语字典aqq,最新的英语qq网名
  15. 通过云打码实现验证码识别
  16. Python将头像照片转换为漫画,采用GAN深度学习,无噪点
  17. 日期和时间范围区间怎么查询
  18. C/C++ —— 什么是定义?什么是声明?
  19. 帕斯卡三角形 python
  20. 串口通信学习(GPS模块)2021.5.10

热门文章

  1. mySAP标准培训教材全套列表
  2. windows上git clone命令速度过慢问题的解决
  3. matplotlib 无法显示中文字体的解决方法
  4. 智能推荐系统之数据预处理
  5. 速成pytorch学习——11天. 使用GPU训练模型
  6. php 换一换 功能,vue换一换功能原型
  7. 如何在opengl用代码绘制英文_如何用AIPS直接绘制服装款式图(线稿)
  8. sqlmap md5怎么解密_UC浏览器代理流量解密
  9. Tiktok培训可以去学习吗?
  10. 机器学习部分内容总结