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来验证其可行性.关键是如 ...
随机推荐
- 2019.6.28 校内测试 T4 【音乐会】达拉崩吧·上
考试的一道附加题~ 一看题目描述:把区间[l,r]里每个数异或上x,求区间[l,r]里所有数的异或和,这明显的要用数据结构或RMQ吧. 恩,所以正解就是线段树啦,至于树状数组行与否,不知道~ wate ...
- Ajax请求如何设置csrf_token
1. 方式一 通过获取隐藏的input标签中的csrfmiddlewaretoken值,放置在data中发送. $.ajax({ url: "/cookie_ajax/", typ ...
- 3.5寸1.44M软盘结构
结构: 划分: 簇:磁盘驱动器在向磁盘读取和写入数据时,要以扇区为单位.在磁盘上,DOS操作系统是以“簇”为单位为文件分配磁盘空间的.硬盘的簇通常为多个扇区,与磁盘的种类.DOS 版本及硬盘分区的大小 ...
- 使用linux中,最让人无语的是软件源
使用linux的最大的障碍是软件源的配置和系统的安装,这两个搞定了,坚持使用下去都不是问题,如果实在不行,还可以win10下的linux子系统可以作为基本的使用.下面记录两个软件源: Found 2 ...
- Ubuntu常用命令及git常用命令
1. CMakeLists.txt中指定OpenCV路径 set(OPENCV_DIR /***/***/opencv-2.4.9) 2. cmake工程编译安装 mkdir build cd bui ...
- 重读APUE(15)-pthread_cond_wait与while循环
即使pthead_cond_wait()和pthread_cond_timewait()没有错误返回,等待的条件也可能是假的:即使pthread_cond_timewait()返回了超时错误,关联的条 ...
- Mapping Pocos
Mapping Pocos Example Pocos/Mappings public class Note { public int Id { get; set; } public DateTime ...
- CDH 更换 HDFS 数据目录
先停止 HDFS 角色. 数据文件位置默认在 /dfs/ 中,这里配置 NameNode.SecondaryNameNode.DataNode 数据目录. 先在所有 HDFS 的主机上把数据拷贝过去, ...
- app微信支付的集成步骤
1.引用地址 //微信支付 compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' 2.注册 private IWXAPI api ...
- RTM of ML.NET
Announcing ML.NET 1.0