*HDU1325 并查集
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22403 Accepted Submission(s): 5088
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
7 4 7 8 7 6 0 0
5 3 5 6 5 2 0 0
//判断两点,无环,非森林。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int fat[];
int nod[];
int find(int x)
{
if(fat[x]!=x)
fat[x]=find(fat[x]);
return fat[x];
}
void connect(int x,int y)
{
int xx=find(x),yy=find(y);
if(xx!=yy)
fat[xx]=yy;
}
int main()
{
int a,b;
for(int i=;i<;i++)
fat[i]=i;
int t=,k=,flag=;
memset(nod,,sizeof(nod));
while(scanf("%d%d",&a,&b))
{
if(a<&&b<) break;
if(a==&&b==)
{
if(!flag) //判断是否是森林
{
int tem=;
for(int i=;i<;i++)
{
if(!nod[i]) continue;
if(tem==) tem=find(i);
else if(tem!=&&find(i)!=tem)
{
flag=;
break;
}
}
}
if(flag) printf("Case %d is not a tree.\n",k);
else printf("Case %d is a tree.\n",k);
t=;
flag=;
k++;
for(int i=;i<;i++)
fat[i]=i;
memset(nod,,sizeof(nod));
continue;
}
nod[a]=;nod[b]=;
if(flag) continue;
if(fat[b]!=b||find(a)==b) //不能是环:b之前不能有父亲,a,b不能互相连接
{
flag=;
continue;
}
fat[b]=a;
connect(b,a);
}
return ;
}
*HDU1325 并查集的更多相关文章
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28838 Accepted: 9843 -& ...
- 并查集树数据结构hdu1325
我的解法就是去构造了一棵树 以数组的存储方式 数组的值存放节点的根. 排除空树 剩下的就是出现环和多根节点的情况 也就是排除森林和有一个节点多个入度的情况 排除森林就用到了并查集 也就是便利数组让其仅 ...
- 并查集(Union-Find) 应用举例 --- 基础篇
本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
随机推荐
- linuxmint 17安装scim输入法
安装输入法框架: sudo apt-get install scim 安装输入法: sodu apt-get install scim-pinyin 此时启动的输入法的候选词框不随着光标移动,需要安装 ...
- 【总结2】PhpStorm利用XDebug调试PHP技巧
在写完 [总结1]PhpStorm配置XDebug(远程)调试PHP 后,发现PhpStorm调试PHP还有一个技巧,如下图所示: 路径:File -> Settings -> Langu ...
- PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令
exec 或者 system 都可以调用cmd 的命令 直接上代码: <?php /** 打开windows的计算器 */ exec('start C:WindowsSystem32calc.e ...
- sql语句修改列
1.修改列名的常用语句 exec sp_rename '[dbo].[Table_1].[birthday]','birth' --将表中列名为birthday改为birdh 属性不变,用的是数据库 ...
- Java总结篇系列:Java多线程(一)
多线程作为Java中很重要的一个知识点,在此还是有必要总结一下的. 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程 ...
- poj 1192
此题亦一眼看出算法,一次AC. 没什么好讲的,就是一个普通的树形动规. 用dp[n][0]表示n号顶点不取时的最大值,dp[n][1]表示n号顶点取时的最大值. dp[n][0]=max{dp[x][ ...
- fdatool 设计IIR滤波器
[B,A] = sos2tf(SOS);K = cumprod(G);k=K(end); [y_out] = filter(B, A, win_up_data, []) .*k;
- angularjs向后台传参,后台收不到数据
angularjs中封装了一个$http服务,用来请求远程资源 参见:HTTP API 其中封装过的$http.post和$http.get使用起来比较方便 后台是php,用$_POST['name' ...
- PHP获取一年有几周以及每周开始日期和结束日期
最近接了一个项目,其中有一需求是用php获取一年有几周以及每周开始日期和接触日期.在网上找些资料没有合适的,于是自己做了一份,下面通过两种方式实现PHP获取一年有几周以及每周开始日期和结束日期 代码一 ...
- Redis 复制、Sentinel的搭建和原理说明
背景: Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端) ...