hdu---(1325)Is It A Tree?(并查集)
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14525 Accepted Submission(s): 3232
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 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.
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).
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 2 is a tree.
Case 3 is not a tree.
/*
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 2 3 2 4 2 5 2 0 0
1 2 3 4 4 3 0 0
0 0
1 1 0 0
1 2 2 1 0 0
1 2 2 3 3 4 4 1 0 0
1 2 2 3 3 1 5 6 0 0
2 3 0 0
-1 -1
*/
答案:
/*
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
Case 4 is not a tree.
Case 5 is not a tree.
Case 6 is a tree.
Case 7 is not a tree.
Case 8 is not a tree.
Case 9 is not a tree.
Case 10 is not a tree.
Case 11 is a tree. */
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
bool hasp[];
struct node{
int father,rank;
};
node root[];
void init(){
int i;
root[].rank=;
root[].father=;
for(i=;i<;i++){
root[i].father=i;
root[i].rank=;
}
}
int find(int a){
while(a!=root[a].father)
a=root[a].father;
return a;
}
void Union(int a,int b){
/*a->b*/
root[b].father=a;
root[a].rank+=root[b].rank;
}
int main(){
freopen("test.in","r",stdin);
//system("call test.in");
int a,b,cnt,x,y,cas=,tem;
bool flag;
while(){
flag=false; //初始化为无环
memset(hasp,,sizeof(hasp));
tem=cnt=;
init();
while(scanf("%d%d",&a,&b)&&(a+b!=)){
if(a+b<) return ;
if(!flag)
{
x=find(a);
y=find(b);
if(x==y) flag=;
else Union(a,b);
if(tem==) tem=a;
if(!hasp[a]) hasp[a]= , cnt++ ;
if(!hasp[b]) hasp[b]= , cnt++ ;
}
}
/* cnt记录了点的个数 */
if(root[find(tem)].rank==cnt&&!flag)
printf("Case %d is a tree.\n",cas++);
else
printf("Case %d is not a tree.\n",cas++);
}
return ;
}
hdu---(1325)Is It A Tree?(并查集)的更多相关文章
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu 1325 Is It A Tree? 并查集
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
随机推荐
- BeagleBone Black– 智能家居控制系统 LAS - ESP8266 UDP 服务
NodeMCU 的文档里面终于发现,ESP8266 的GPIO 2 确实是 PIN 4,GPIO 0 是 PIN 3. https://github.com/nodemcu/nodemcu-firmw ...
- NPOI大数据分批写入同个Excel
实现过程: 要导出来的数据库数据量很大,一次取出来压力有点大,故分批取出来,导入到同一个Excel. 因为Excel2003版最大行数是65536行,Excel2007开始的版本最大行数是104857 ...
- Spark.ML之PipeLine学习笔记
地址: http://spark.apache.org/docs/2.0.0/ml-pipeline.html Spark PipeLine 是基于DataFrames的高层的API,可以方便用户 ...
- Quartz.Net 调度框架配置介绍
在平时的工作中,估计大多数都做过轮询调度的任务,比如定时轮询数据库同步,定时邮件通知等等.大家通过windows计划任务,windows服务等都实现过此类任务,甚至实现过自己的配置定制化的框架.那今天 ...
- 常用的jquery
获取一组radio被选中项的值 var item = $('input[@name=items][@checked]').val(); 获取select被选中项的文本 var item = $(&qu ...
- CDN学习笔记二(技术详解)
一本好的入门书是带你进入陌生领域的明灯,<CDN技术详解>绝对是带你进入CDN行业的那盏最亮的明灯.因此,虽然只是纯粹的重点抄录,我也要把<CDN技术详解>的精华放上网.公诸同 ...
- js 读写cookie。不同路径会储存各自的cookie。而 在v.net环境下读写是在 / 根目录。
所以如果全站不分path 的 话.应该显示的写上 path .设置为根目录 function setCookie(name, value) { document.cookie = name + &qu ...
- thinkphp3.2+PHPExcel导出查询数据到excel表格的实例
首先下载PHPExcel插件,我们需要把PHPExcel.php和PHPExcel文件夹放到D:\XAMPP\htdocs\fsxb\ThinkPHP\Library\Vendor\PHPExcel目 ...
- Python类、模块、包的区别
类 类的概念在许多语言中出现,很容易理解.它将数据和操作进行封装,以便将来的复用. 模块 模块,在Python可理解为对应于一个文件.在创建了一个脚本文件后,定义了某些函数和变量.你在其他需要这些功能 ...
- pkg-config问题:
pkg-config是一个工具,可以用于检测相应的依赖环境. pkg-config用来检索系统中安装库文件的信息,典型的是用作库的编译和连接.一般来说,如果库的头文件不在/usr/include目录中 ...