POJ 1308
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的更多相关文章
- POJ 1308&&HDU 1272 并查集判断图
HDU 1272 I - 小希的迷宫 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- 杭电 1272 POJ 1308 小希的迷宫
这道题是我学了并查集过后做的第三个题,教我们的学姐说这是并查集的基础题,所以有必要牢牢掌握. 下面就我做这道题的经验,给大家一些建议吧!当然,我的建议不是最好的,还请各位大神指出我的错误来,我也好改正 ...
- 并查集判树 poj 1308
例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...
- 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 并查集的运用 ...
- (并查集)POJ 1308 & HDU 1325
一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只 ...
- HDU 1325,POJ 1308 Is It A Tree
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- 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 ...
- POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18778 Accepted: 6395 De ...
- 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 ...
随机推荐
- log4j2 使用
转载自 Blog of 天外的星星: http://www.cnblogs.com/leo-lsw/p/log4j2tutorial.html Log4j 2的好处就不和大家说了,如果你搜了2,说明你 ...
- Yii2 前台用户与后台用户分离
1.将common/models/User.php在当前目录下copy一份,命名为Admin.php,修改类的名称为Admin 2.将common/models/LoginForm.php复制到bac ...
- p命名空间的使用(不推荐用)
xmlns:p="http://www.springframework.org/schema/p" p:没有xsd文件,直接加上面那句就好了 <!-- singleton和p ...
- maven 错误解决办法集
一.mavenFailed to execute goal org.apache.maven.plugins:maven-surefire-plugin解决方法 1.测试代码没有获得通过,可以尝重命名 ...
- Javaee----重新回顾servlet
最近面临找工作,不得不回顾一下java servelt . 发现lz的基本功还是很差 1. 每一个servlet都必须实现servlet接口,GenericServlet是个通用的.不特定于任何协议的 ...
- Nginx安装配置(转)
Nginx 安装配置 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/ ...
- CentOS的SSH,Putty配置说明
基本资源: CentOS5.5 (32位) , Mysql6.0 ,Putty ,SSH Step: 1.VMWare中装好CentOS A. 可能存在ifconfig等命令无法正常识别) ...
- 【深入JVM内核—原理、诊断与优化】第2期开课了
[深入JVM内核—原理.诊断与优化]的讲师“葛一鸣”,人称“一哥”,毕业于浙江工业大学,计算机软件与理论专业硕士,是国家认证系统分析师,OCP.2012年出版过<Java程序性能优化>,荣 ...
- Java并发包源码学习之AQS框架(一)概述
AQS其实就是java.util.concurrent.locks.AbstractQueuedSynchronizer这个类. 阅读Java的并发包源码你会发现这个类是整个java.util.con ...
- 【转】Eclipse里项目名有红叉,但是展开后里面又没有红叉叉
Eclipse里项目名有红叉,但是下面的每一个文件都没有红叉 有三种可能: 1:classpath有问题 2:编译级别有问题 3:jar包有问题,我碰过从maven上获取了问题jar包,工程里提示能找 ...