(╭ ̄3 ̄)╭ 小希的迷宫II
(╭ ̄3 ̄)╭ 小希的迷宫II
对于每一幅地图,都需要满足上述所要求的树形结构,这样的设计图才算合格。当只输入0 0,判断为树
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree. 解法一:(只是在小希的迷宫上添加了入度条件即可)
#include <iostream>
#include <stdio.h>
using namespace std;
#define Max 100100
int ID[Max];
int InD[Max];
int Out_Num;
int Pio[Max];
int SIGN;
int P_Num;
void Cread(int N)
{
for(int i=;i<=N;i++){ID[i]=i;Pio[i]=;InD[i]=;}
}
int Find(int x)
{
int TMD=x,TMP;
while(TMD!=ID[TMD])TMD=ID[TMD];
while(x!=TMD)
{
TMP=ID[x];
ID[x]=TMD;
x=TMP;
}
return x;
}
void Update(int a,int b)
{
if(Pio[a]){P_Num++;Pio[a]=;}
if(Pio[b]){P_Num++;Pio[b]=;}
if(InD[b]){Out_Num++;InD[b]=;}
int A=Find(a);
int B=Find(b);
if(A!=B)
{
ID[A]=B;
SIGN++;
}
else SIGN=Max;
}
int main()
{
int T,N,M,i,j,a,b,t=;
Cread(Max);SIGN=;P_Num=;Out_Num=;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a<&&b<)break;
if(a==&&b==)
{
// printf("%d %d %d\n",SIGN,Out_Num,P_Num);
if((SIGN==P_Num-||P_Num==)&&Out_Num==SIGN)
printf("Case %d is a tree.\n",t++);
else
printf("Case %d is not a tree.\n",t++);
Cread(Max);SIGN=;P_Num=;Out_Num=;
continue;
}
Update(a,b);
}
return ;
}
正解:(只需要根据树的定义,判断入度即可。。。)
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int d[];
bool bo[];
int bianshu=;
void init(){
memset(d,,sizeof(d));
memset(bo,,sizeof(bo));
bianshu=;
}
int main(){
int x,y,cas=;
while(~scanf("%d%d",&x,&y))
{
if(x==-&&y==-){
break;
}
if(x==&&y==){
int bobo=;
int i;
for(i=;i<=;i++){
if(bo[i]&&d[i]==){
if(bobo==) bobo=;
else break;
}else
if(bo[i]&&d[i]!=){
break;
}
}
if(i<=) printf("Case %d is not a tree.\n",cas++);
else printf("Case %d is a tree.\n",cas++);
init();
continue;
}
d[y]++;
bo[x]=true;
bo[y]=true;
bianshu++;
}
return ;
}
(╭ ̄3 ̄)╭ 小希的迷宫II的更多相关文章
- ( ̄y▽ ̄)~ 智能手机II
( ̄y▽ ̄)~ 智能手机II TimeLimit: 3000/1000 MS (Java/Others) MenoryLimit: 32768/32768 K (Java/Others) 64-bi ...
- HDU 1272 小希的迷宫 并查集
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1272 小希的迷宫
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 小希的迷宫(MST单棵树判断法则)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1272 小希的迷宫 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 第二条并查集,和畅通工程的解法类似.判断小希的迷宫不符合条件,即有回路.我的做法是,在合并两个集 ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- HDU 1272 小希的迷宫 (并查集)
小希的迷宫 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/L Description 我们的小伙伴Bingo身为大二学长,他乐于 ...
- 小希的迷宫(HDU 1272 并查集判断生成树)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 小希的迷宫--hdu1272(并查集)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
随机推荐
- hdu1032
#include <iostream> using namespace std; int main() { int a,b,t,i,max; while(cin >> a &g ...
- 驱动相关Error
驱动中 fltKernel.h报 EPROCESS和PETHREAD重定义异常解决办法 驱动编写中经常会莫名出现 error C2371: 'PEPROCESS' : redefinition; di ...
- IOS常遇问题个人收藏网址指南
代码适配Masonry使用的详细介绍: http://blog.csdn.net/majiakun1/article/details/51160339 Masonry使用注意篇: http://www ...
- Python基础(五)-函数
函数: 1.定义与使用: def 函数名(参数): "函数_文档字符串" 函数体 ... return [表达式] ## def:表示函数的关键字 函数名:函数名称,根据函数名调用 ...
- sqlserver负载均衡
http://www.cnblogs.com/gaizai/p/3644510.html
- NIO和IO(转)
java NIO由以下几个核心部分组成: Channels(通道) Buffers(缓冲区) Selectors(选择器) 其他 Channel和Buffer: 所有的IO再NIO中都从一个Chann ...
- css3技巧属性之text-overflow
text-overflow:clip | ellipsis 默认值:clip 取值: clip: 当对象内文本溢出时不显示省略标记(...),而是将溢出的部分裁切掉. ellipsis: 当对象内文本 ...
- modelsim+win环境下systemverilog调用c函数
最近为了验证一个ip,需要将ip的输出数据与c model的数据比对,之前采用的是将仿真结果输出,用perl读取结果,与c的输出结果比对,这样做也可以,但是在做遍历测试时,由于数据量较大,就显得不方便 ...
- html中 iframe子页面 与父页面之间的方法调用 ;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- pull类型消息中间件-消息服务端(三)
部署架构 消息存储 存储结构 MetaQ的存储结构是一种物理队列+逻辑队列的结构.如下图所示: Producer生产消息,根据消息的topic选择topic对应某一个分区,然后发送到这个分区对应的Br ...