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. struts2基础篇(1)

    一.Struts2简介Struts2以WebWork优秀的设计思想为核心,吸收了Struts1的部分优点,建立了一个基于WebWork和Struts1的MVC框架. 二.搭建Struts2开发环境2. ...

  2. 为什么构造器不能是abstract, static, final, native or synchronized的?

    Unlike methods, a constructor cannot be abstract, static, final, native  or synchronized. 1. A const ...

  3. Hadoop Fsimage 和 editlog

    在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...

  4. PHP基础之 数组(二)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. CSS 补充

    属性选择器下面的例子为带有 title 属性的所有元素设置样式:[title]{ color:red;} <h1>可以应用样式:</h1><h2 title=" ...

  6. 汉字转拼音(pinyin4j)

    1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...

  7. C\C++ sizeof 陷阱&&总结

    今天使用动态数组,本来想通过sizeof 获取动态数据,结果出现了错误. 先对自己做个测试,能做出下面这个题目,并做出合理解释,可以不用往下看了. ][]; cout<< cout< ...

  8. js兼容注意事项--仅供参考

    做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...

  9. 解决IE6不支持position:fixed的bug

    /*完整代码 */ /* 除IE6浏览器的通用方法 */ .ie6fixedTL { position: fixed; left:; top:; } .ie6fixedBR { position: f ...

  10. 创建第一个JBPM6项目并且运行自带的helloword例子(JBPM6学习之三)

    1. 打开Eclipse,右键New JBPM Project 项目,在项目名称里面填写一个项目名字,如“TestJbpm6”,然后下一步,知道Finish完成(里面会使用我们配置的运行环境). 2. ...