http://poj.org/problem?id=1308

题意:判断这是不是一棵树。

思路:这个题还是有部分坑点的。首先空树也是一棵树,还有森林不是树。

关于森林的判断我是利用并查集把每一个点压缩路径,看一共有几个原始点,超过一个,则不是树是森林。

关于并查集

寻找以及压缩的代码

 int Find(int x)
{
int _b,int _x=x;
while(belg[_x]!=_x) //压缩路径,找到它的最顶端的点。
{
_x=belg[_x];
}
while(belg[x]!=x) //把这一系列的点的父亲节点都更新到最顶端的点。
{
_b=belg[x];
belg[x]=_x;
x=_b;
}
return _x;
}

关于合并

 int Union(int i,int j)
{
if(rand()&) //随机分配
{
belg[j]=i;
}
else
{
belg[i]=j;
}
}
 #include <string.h>
#include <stdio.h>
#include <iostream>
#define l 10010 using namespace std; int belg[l]; int fin(int x)
{
int _b,_x=x;
while(belg[_x]!=_x&&belg[_x])
{
_x=belg[_x];
}
while(belg[x]!=x&&belg[x])
{
_b=belg[x];
belg[x]=_x;
x=_b;
}
return _x;
} int main()
{
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int m,n,ans,cas,father;
bool mark[l];
cas=;
while(scanf("%d%d",&m,&n),m!=-||n!=-)
{
ans=,father=;
memset(belg,,sizeof(belg));
memset(mark,false,sizeof(mark));
belg[n]=m;
mark[n]=true;
if(m==n&&m!=) ans=;
if(m==n&&m==) ans=;
else while(scanf("%d%d",&m,&n),m||n)
{
if(belg[m]==n) {ans=;continue;}
if(belg[n]) ans=;
if(n==m) ans=;
if(!belg[n]&&n!=m) belg[n]=m;
fin(n);
mark[n]=true;
}
for(int i=;i<;i++)
if(mark[i]) fin(i);
for(int i=;i<;i++)
{
if(father==&&mark[i]) {father=belg[i];continue;}
if(mark[i]&&belg[i]!=father) {
ans=;
break;
}
}
if(ans) printf("Case %d is a tree.\n",++cas);
else printf("Case %d is not a tree.\n",++cas);
}
return ;
}

测试数据:

1 2 2 3 3 1 5 6 0 0

0 0

0 0

1 2 3 4 4 5 5 3 0 0

2 1 3 1 4 1 5 1 0 0

2 1 3 1 4 1 0 0

1 2 4 5 5 4 0 0

1 2 1 3 1 4 1 5 0 0

-1 -1

答案

Case 1 is not a tree.

Case 2 is a tree.
 Case 3 is a tree.
 Case 4 is not a tree.
 Case 5 is not a tree.
 Case 6 is not a tree.
 Case 7 is not a tree.
 Case 8 is a tree.

POJ 1308的更多相关文章

  1. POJ 1308&&HDU 1272 并查集判断图

      HDU 1272 I - 小希的迷宫 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  2. 杭电 1272 POJ 1308 小希的迷宫

    这道题是我学了并查集过后做的第三个题,教我们的学姐说这是并查集的基础题,所以有必要牢牢掌握. 下面就我做这道题的经验,给大家一些建议吧!当然,我的建议不是最好的,还请各位大神指出我的错误来,我也好改正 ...

  3. 并查集判树 poj 1308

    例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...

  4. POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫

    POJ题目网址:http://poj.org/problem?id=1308 HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272 并查集的运用 ...

  5. (并查集)POJ 1308 & HDU 1325

    一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只 ...

  6. HDU 1325,POJ 1308 Is It A Tree

    HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...

  7. 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 ...

  8. POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18778   Accepted: 6395 De ...

  9. HDU ACM 1325 / POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. wampserver 2.5安装pear win8.1

    集成环境的悲伤啊~ 本来看到pear想试试 结果发现根本没有go-pear.bat 自己的环境 都是 系统win 8.1 php 5.5.12 mysql  5.6.17 apache 2.4.9   ...

  2. 通过LVS+Keepalived搭建高可用的负载均衡集群系统

    1. 安装LVS软件      (1)安装前准备操作系统:统一采用Centos6.5版本,地址规划如下: 服务器名 IP地址 网关 虚拟设备名 虚拟ip Director Server 192.168 ...

  3. TFS2012 安装 配置笔记

    TFS2012安装   具体请看文档..   http://yunpan.cn/cmt4X6S7TjEgq  访问密码 464e     TFS2012配置 环境:VS2012  SQL2008  T ...

  4. BootStrap学习------栅格

    使用Bootstrap前端框架-栅格 要点 1.使用Bootstrap需要引入的css和js: (1)bootstrap.min.js (2)bootstrap.min.css 2.栅格系统需要通过& ...

  5. c# 中使用memcached

    1.首先下载memcached 服务端 2.使用Enyim.Caching .Net 客户端 3.配置web.config    <sectionGroup name="QuickBo ...

  6. winScp上传文件时,如何过滤制定文件

    在用winScp上传文件时,有些文件不想上传到服务器上.怎么办呢? 比如我希望过滤.svn .git文件和目录怎么操作呢? 第一步:在菜单上选中选项,点击选项. 第二步,点击传输->编辑 第三步 ...

  7. Memcached基础知识

    主要内容: Memcached基本的工作原理 Memcached的两阶段哈希 Memcached的数据存储方式 Memcached新建Item分配内存过程 Memcached的数据过期方式 Memca ...

  8. css3 实现逐帧动画

    css3 实现逐帧动画 实现逐帧动画需要使用到的是Animation动画,该CSS3的Animation有八个属性:分别是如下:1: animation-name2: animation-durati ...

  9. HDU 2007

    /*杭电ACM ID:2007*/ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int in1, in2 ...

  10. UI第二节——UIButton详解

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...