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来验证其可行性.关键是如 ...
随机推荐
- 又见回文 (SDUT 2560)
Problem Description "回文串"是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.现在呢, ...
- Ubuntu 14.04 更换为阿里云源
#备份sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak#编辑sudo vim /etc/apt/sources.list,清空后,加入以下 ...
- ERRORS: ?: (corsheaders.E013) Origin '*' in CORS_ORIGIN_WHITELIST is missing scheme or netloc HINT:
报错信息 ERRORS: ?: (corsheaders.E013) Origin '*' in CORS_ORIGIN_WHITELIST is missing scheme or netloc H ...
- Ubuntu 14.04 64bit中永久添加DNS的方法
第一种方法修改如下文件,默认是空的sudo vim /etc/resolvconf/resolv.conf.d/base在里面加入你想添加的DNS服务器,一行一个nameserver 114.114. ...
- Js 之获取对象key值
var date = Object.keys(data); Object.keys( ) 会返回一个数组,数组中是这个对象的key值列表 所以只要Object.keys(a)[0], 就可以得只包含一 ...
- 基本PSO算法实现(Java)
一.算法流程 Step1:初始化一群粒子(粒子个数为50个),包括随即位置和速度: Step2:计算每个粒子的适应度fitness: Step3:对每个粒子,将其适应度与其进过的最好位置(局部)pbe ...
- $this->request->post 和input 区别
$this->request->post 和input 区别 Request是对象,而input是内置函数 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论
- [Java]借助PrintWriter类和StringWriter类,取出异常堆栈信息放入字符串中
在程序开发中,有时我们不仅需要将异常堆栈信息打印在控制台里或是log里,可能还需要将它存在String中,再送到合适的地方,如错误页面,数据库等. 要取异常堆栈信息,具体的函数就是: /** * Ge ...
- LightGBM两种使用方式
原生形式使用lightgbm(import lightgbm as lgb) import lightgbm as lgb from sklearn.metrics import mean_squar ...
- ROS 常用命令
1.查看网卡接口: /interface print 2.给网口添加IP地址: /ip address add address=192.168.1.254/24 interface=ether1 3 ...