链接:https://ac.nowcoder.com/acm/problem/105905
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 10000K,其他语言20000K
64bit IO Format: %lld
题目描述
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.
输入描述:
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.
输出描述:
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).
示例1
输入
复制
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
输出
复制
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree

题解:

题意:判断给的是不是树
我们想想树的性质,每个点都有一个父亲节点,除了根节点
前者我们可以用并查集维护,后者可以用入度出度来判断,因为根节点只有出度,入度为0,而其他点的入度为1
为了避免是森林,我们还应该要求所有点必须联通
对了,空树也为树

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <math.h>
#include <string>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#define maxn  10010
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;typedef long long ll;
ll mod = 998244353;
using namespace std;
int f[maxn];
int in[maxn];
bool visited[maxn];
int ifind(int x){if(x==f[x]) return x;return f[x]=ifind(f[x]);
}
void imerge(int x,int y){int dx=ifind(x);int dy=ifind(y);if(dx!=dy) f[dx]=dy;return ;
}int main()
{int n,m;int z=1;while(cin>>n>>m&&n!=-1){for(int i=0;i<maxn;i++) f[i]=i;memset(visited,false,sizeof(visited));memset(in,0,sizeof(in));bool flag=true;if(n==0&&m==0){printf("Case %d is a tree.\n",z++);continue;}while(n||m){visited[n]=true;visited[m]=true;in[m]++;imerge(n,m);scanf("%d%d",&n,&m);}int ans=0;for(int i=1;i<maxn;i++){if(visited[i]&&in[i]==0){ans++;}if(in[i]>=2) flag=false;}if(ans!=1) flag=false;if(flag==false) printf("Case %d is not a tree.\n",z++);else printf("Case %d is a tree.\n",z++);}return 0;
}

【每日一题】8月27日题目精讲 Is It A Tree?相关推荐

  1. 牛客网 每日一题 7月23日题目精讲—wpy的请求

    来源:牛客网: 文章目录 wpy的请求 题解: 代码: wpy的请求 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K Special Judge ...

  2. 牛客网【每日一题】5月27日题目精讲 货币系统

    链接: 文章目录 题目描述 题解: 代码: 题目描述 在网友的国度中共有n种不同面额的货币,第i种货币的面额为a[i],你可以假设每一种货币都有无穷多张.为了方便,我们把货币种数为n.面额数组为a[1 ...

  3. 【每日一题】4月27日题目精讲 Removal

    链接: 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048576K 64bit IO Format: %lld 题目描述 Bobo has a seque ...

  4. 牛客网 【每日一题】7月27日题目精讲—乌龟棋

    来源:牛客网: 乌龟棋 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 文章目录 乌龟棋 题目描述 ...

  5. 【每日一题】8月28日题目精讲 编号

    [每日一题]8月28日题目精讲 编号 链接:https://ac.nowcoder.com/acm/problem/19925 来源:牛客网 题目描述 你需要给一批商品编号,其中每个编号都是一个7位1 ...

  6. 【每日一题】7月17日题目精讲—BOWL 碗的叠放

    [每日一题]7月17日题目精讲-BOWL 碗的叠放 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld ...

  7. 【每日一题】7月15日题目精讲—生日快乐

    [每日一题]7月15日题目精讲-生日快乐 [SCOI2009]生日快乐 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO For ...

  8. 【每日一题】7月13日题目精讲—Kingdom

    [每日一题]7月13日题目精讲-Kingdom 文章目录 题目描述 题解: 代码: 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 1048576K,其他语言2097152K 64bi ...

  9. 【牛客每日一题】tokitsukaze and Soldier 题目精讲 贪心、优先队列、堆

    链接:https://ac.nowcoder.com/acm/problem/50439 来源:牛客网 ACM在线模板 今天才发现牛客推出了一个每日一题的版块,3月25号就开始了,今天才发现,赶紧补救 ...

最新文章

  1. 搭建Harbor 2.x仓库 - docker私仓搭建
  2. 【正一专栏】贾乃亮发文后李小璐会如何回应?
  3. boost::gil::view_is_basic用法的测试程序
  4. Mybatis中输入输出映射和动态Sql
  5. 论文浅尝 | 重新审视语言模型与知识库的关系
  6. c# 网口相机可以通过_电脑可以跑安卓9.0了!完全免费
  7. Ranger-Solr审计日志安装
  8. 洛谷 P1352 没有上司的舞会【树形DP/邻接链表+链式前向星】
  9. 无心剑随感《最完美的图形——圆》
  10. python pca降维_协方差矩阵的计算、PCA
  11. java中的流思想_Java8新特性 Stream流式思想(二)
  12. matlab相机标定
  13. Python编程:pycharm开发工具汉化步骤
  14. PADS Logic原理图设计
  15. Linux 下重新挂载分区方法
  16. 3D dungeon(BFS)
  17. C++课程设计-失物招领系统
  18. Linux不是Windows(转载,强烈推荐…
  19. java毕业生设计学校医院预约系统计算机源码+系统+mysql+调试部署+lw
  20. jsp+ssm二手书图书回收捐赠管理系统springboot

热门文章

  1. 史上最神奇的公式,竟然藏着这么多秘密!
  2. 为什么AI工程师成为当前薪资最高的技术岗位
  3. 如果每一种语言都对应一种女生,你会喜欢哪一个?
  4. linux docker导入镜像,Docker镜像的导入和导出
  5. oracle 数字处理函数,Oracle函数-单行函数-数字、日期、日期处理函数
  6. html让ul的li自动居中,css ul li导航菜单居中问题解决方法
  7. java proguard 使用_一步步教你使用Proguard混淆Java源代码
  8. java emoji编码转换_java转换emoji表情
  9. jpi多表联查_数据库两表联查、多表联查,多重联查
  10. 怎样用python批量处理文件夹_python批量处理文件或文件夹