POJ1308 Is It A Tree?
题目大意:和HDU1272-小希的迷宫题目一样,
Input
整个文件以两个-1结尾。
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1000005 using namespace std; int father[MAX],vis[MAX]; int Find(int x)
{
while(x!=father[x])
{
x=father[x];
}
return x;
} int main()
{
int a,b,ok,i,k,cnt=; while(scanf("%d%d",&a,&b),a!=- || b!=-)
{
k=;
memset(vis,,sizeof(vis)); vis[a]=;
vis[b]=; ok=; if(!a && !b)
{
printf("Case %d is a tree.\n",cnt++);
continue;
} for(i=; i<MAX; i++)
father[i]=i; int x=Find(a);
int y=Find(b); if(x!=y)
{
father[x]=y;
} else
ok=; while(scanf("%d%d",&a,&b),a||b)
{
vis[a]=;
vis[b]=;
int x=Find(a);
int y=Find(b); if(x!=y)
{
father[x]=y;
}
else
ok=;//标记一下
} if(ok)//当ok=0时就不进入了,节省下时间
{
for(i=;i<MAX;i++)
{
if(vis[i] && father[i]==i)
k++;
}
} if(k>)
ok=; if(ok)
printf("Case %d is a tree.\n",cnt++);
else
printf("Case %d is not a tree.\n",cnt++);
} return ;
}
POJ1308 Is It A Tree?的更多相关文章
- poj1308 Is It A Tree?(并查集)详解
poj1308 http://poj.org/problem?id=1308 题目大意:输入若干组测试数据,输入 (-1 -1) 时输入结束.每组测试数据以输入(0 0)为结束标志.然后根据所给的 ...
- POJ1308——Is It A Tree?
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22631 Accepted: 7756 De ...
- POJ-1308 Is It A Tree?(并查集判断是否是树)
http://poj.org/problem?id=1308 Description A tree is a well-known data structure that is either empt ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- Is It A Tree?----poj1308
http://poj.org/problem?id=1308 #include<stdio.h> #include<string.h> #include<iostream ...
随机推荐
- 【转载】将python脚本打包成exe文件
exe文件也就是可以直接执行的文件.通常我们编好的带py后缀的脚本文件都是需要在有python的环境下执 行,每次通过Win + R打开运行窗口再输入powershell打开控制台,再千辛万苦地cd ...
- iOS 数组字典操作
iOS开发中需要大量对dictionary和array进行操作,因此我们需要一种更加安全可靠的操作方法来避免不必要的crash.当然可以通过自定义dictionary 和array重载增删改查的方法来 ...
- Cash Machine
Problem Description A Bank plans to install a machine for cash withdrawal. The machine is able to de ...
- install font
哪些字体包含国际音标呢? 在微软的Windows与Office的2000或以上版本中分别带有Lucida Sans Unicode和Arial Unicode MS两种字体(以下分别简称LSU和AUM ...
- SqlSever 查询基本
查询语句: SQL sever 查询语句: 1.查询所有字段: select * from UserInfo 2.条件筛选 (如查询UserInfo中的UserName) select UserNam ...
- 不安装oracle客户端,如何运行sqlplus
1.软件下载 http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html 下载如下三个包: oracle-instantc ...
- Spring的后置处理器BeanPostProcessor
一.BeanPostProcessor接口的作用 如果我们需要在Spring容器完成Bean的实例化.配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProce ...
- centos7.2自带的php5.4升级为5.6
参考:http://jingyan.baidu.com/article/380abd0a1f176c1d91192c4b.html 今天在新购的阿里云上部署个phpmyadmin,结果显示了个如下信息 ...
- java对象和json对象之间互相转换
import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.Li ...
- hdu_5787_K-wolf Number(数位DP)
题目链接:hdu_5787_K-wolf Number 题意: 给你一个区间,让你找满足任意k个数位内都没有相同的数字的个数 题解: 因为k不大,就直接将当前pos的前k-1个数传进去就行了 #inc ...