HDU 1325 有根树的判断
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33083 Accepted Submission(s): 7574
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1325
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
题意
给你一些有向边,问你这些点是否构成一颗有根树。
题解
有根树特点:
1.除根点入度为0外,其余点入度均为1
2.没有环
无根树特点:
1.没有环
2.联通
奇环内向数特点:
1.入度全为1
2.联通
我们要做的是有根树,那么用并查集处理环,用set或者map处理每个点的入度即可。
代码
#include<bits/stdc++.h>
using namespace std;
#define N 100050
int find(int fa[],int x)
{
if (x==fa[x])return x;
return fa[x]=find(fa,fa[x]);
}
bool pd()
{
static int f[N],bl[N];
set<int>Q;
memset(f,0,sizeof(f));
for(int i=1;i<N;i++)bl[i]=i;
int num=0,x,y,flag=1;
while(1)
{
scanf("%d %d",&x,&y);
if (x+y==0)break;
if (x<0)exit(0);
Q.insert(x);
Q.insert(y);
f[y]++;
int fx=find(bl,x),fy=find(bl,y);
if (fx==fy||f[y]>1)flag=0;
bl[fx]=fy;
}
for(auto it=Q.begin();it!=Q.end();it++) if (f[*it]==0)num++;
if (flag&&num<=1)return 1;
return 0;
}
void work()
{
static int cas=0;
bool k=pd();
printf("Case %d is ",++cas);
if (!k)printf("not ");
printf("a tree.\n");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while(1)work();
}
HDU 1325 有根树的判断的更多相关文章
- hdu 1325 判断有向图是否为树
题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...
- hdu 2108:Shape of HDU(计算几何,判断多边形是否是凸多边形,水题)
Shape of HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- (并查集)POJ 1308 & HDU 1325
一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只 ...
- HDU 1325,POJ 1308 Is It A Tree
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- hdu 1325 && poj 1308 Is It A Tree?(并查集)
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- HDU 1878 欧拉回路(判断欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 题目大意:欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一 ...
- HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...
- HDU 1325(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 3622(二分+2-sat判断可行性)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3622 思路:二分是容易想到的,由于题目中有明显的矛盾关系,因此可以用2-sat来验证其可行性.关键是如 ...
随机推荐
- [Windows]Visual Studio Code个人配置
编辑器要添加的 settings.json 打开这个: { ,//编辑字号 "files.autoSave": "afterDelay",//自动保存 &quo ...
- Maven:禁止编码指定类型的资源文件
[参考文章]:项目编译后dll文件调用出错 maven 在编译或项目时,可能会对资源文件进二次编码(编译前后的文件大小对比即可发现该问题),有些文件(例如:文本文件)可能不会影响我们是用,但是有些文件 ...
- 验证HashSet和HashMap不是线程安全
JAVA集合类: java.util包下的HashSet和HashMap类不是线程安全的, java.util.concurrent包下的ConcurrentHashMap类是线程安全的. 写2个测试 ...
- [java]取当前时间
/** * Get current date time * * @return */ private static String getCurrTime() { SimpleDateFormat sd ...
- 错误注入 异常行为 环境变量或代码动态激活来触发这些异常行为 模拟错误 容错性 正确性 稳定性 宏 本质 macro
小结: 1. 微服务中某个服务出现随机延迟.某个服务不可用. 存储系统磁盘 IO 延迟增加.IO 吞吐量过低.落盘时间长. 调度系统中出现热点,某个调度指令失败. 充值系统中模拟第三方重复请求充值成功 ...
- 《你不知道的JavaScript(上)》笔记——词法作用域
词法作用域是一套关于引擎如何寻找变量以及会在何处找到变量的规则. 词法作用域最重要的特征是它的定义过程发生在代码的书写阶段(假设你没有使用eval() 或 with) 欺骗词法:指修改词法作用域, 欺 ...
- layer快速点击会触发多次回调
场景还原 测试同学反馈点击了一次操作,为什么会有两条操作记录? 我:???? 排查思路 查看日志,看一下是不是发了两次请求,果不其然啊: 并发了,同一时间发送了两次请求,出现了脏写. 原因 系统的co ...
- linux下的什么工具能将DVI文件转换成PostScript文件?
答: dvips,此工具能将由Latex或Tex生成的DVI文件转换成PostScript文件,官网在此
- apt如何列出所有已经安装的软件包
apt如何列出所有已经安装的软件包 转 https://www.helplib.com/ubuntu/article_155294 问题: 我想将所有已安装软件包的列表输出到文本文件中,以便我可以查看 ...
- react native Expo完全基于ScrollView实现的下拉刷新和上拉触底加载
我直接封装成了一个组件 props参数为 static propTypes = { style:PropTypes.object, // 样式 refreshing:PropTypes.bool.is ...