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. JSP指令

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  2. jQuery 取值、赋值的基本方法

    转载:http://www.cnblogs.com/huanhuan86/archive/2012/06/13/2548071.html 获取元素的value值: /*获得TEXT.AREATEXT的 ...

  3. Linux操作、hadoop和sh脚本小结

    近期一直在忙项目上的事情,今天对以前的工作做一个简单的小结.明天就是国庆节啦. 1  脚本可以手动执行,可是crontab缺总是找不到路径? #!/bin/bash. /etc/profile . / ...

  4. golang笔记——命令

    1.GO命令一览 GO提供了很多命令,包括打包.格式化代码.文档生成.下载第三方包等等诸多功能,我们可以通过在控制台下执行 go 来查看内置的所有命令 下面来逐个介绍,也可以详细参考 https:// ...

  5. gitlab一键安装 (转)

    原文地址:http://www.2cto.com/os/201411/353292.html 0 简介bitnami和gitlab bitnami BitNami是一个开源项目,该项目产生的开源软件包 ...

  6. CSS vertical-align 属性

    定义和用法 vertical-align 属性设置元素的垂直对齐方式.该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐

  7. 错误:The Controls collection cannot be modified because the control contains code blocks (i.e. ). .

    用 <%# %>这种写法是写在数据绑定控件中的,之所以用 <%= %>会出现The Controls collection cannot be modified because ...

  8. nginx的那些内置变量

    nginx在配置文件nginx.conf中可以使用很多内置变量,配置如下: location /info { add_header 'Content-Type' 'text/html'; echo & ...

  9. FineUI第十八天---表格之事件的处理

    表格之事件的处理: 1.事件参数: GridPageEventArgs:表格分页事件参数,对应onPageIndexChange事件. NewPageIndex:新页面的索引 GridSortEven ...

  10. DEV控件Grid显示行号

    DEV控件Grid的显示行号需要通过一个事件来设置,具体设置代码为: private void gridView1_CustomDrawRowIndicator(object sender, DevE ...