Is It A Tree?(hdu1325)
题目链接: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
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.
#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)相关推荐
- 【HDU1325】Is It A Tree?(并查集基础题)
有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...
- UVALive5461 UVA615 POJ1308 Is It A Tree?(解法二)【废除!!!】
本文废除!!! 参考链接:UVALive5461 UVA615 POJ1308 ZOJ1268 Is It A Tree?[并查集] Regionals 1997 >> North Ame ...
- 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 ...
- 102. Binary Tree Level Order Traversal
题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...
- Python---哈夫曼树---Huffman Tree
今天要讲的是天才哈夫曼的哈夫曼编码,这是树形数据结构的一个典型应用. !!!敲黑板!!!哈夫曼树的构建以及编码方式将是我们的学习重点. 老方式,代码+解释,手把手教你Python完成哈夫曼编码的全过程 ...
- [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 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 ...
- Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)
Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...
- datagrid底部显示水平滚动_DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强
行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...
最新文章
- Windows Mobile 6.0 SDK和中文模拟器下载
- js php 时间格式化字符串,JS怎么实现字符串与日期的互相转换及日期的格式化
- 据库中事务、会话、线程这几个概念是什么关系
- VC程序中运行其他程序的三种方法
- python创建django项目_搭建Python-Django环境,创建第一个Django项目
- leetcode —— 1079. 活字印刷
- 为了生产iPhone 12,富士康连国庆节也没假放了
- Spark Job Scheduling
- bootstrap不同分辨率显示滚动条_bootstrap基础快速入门-8 响应式显示与隐藏
- 视频教程-大数据编程语言scala讲座-其他
- Rhino基础教程---三管混接(法二、法三)
- mac linux 键盘布局,Macbook Pro 推出中文键盘布局
- 优酷屏幕录制在哪里_手机优酷怎么录制视频
- 计算机专业英语字典aqq,最新的英语qq网名
- 通过云打码实现验证码识别
- Python将头像照片转换为漫画,采用GAN深度学习,无噪点
- 日期和时间范围区间怎么查询
- C/C++ —— 什么是定义?什么是声明?
- 帕斯卡三角形 python
- 串口通信学习(GPS模块)2021.5.10