HDUOJ------(1272)小希的迷宫
小希的迷宫
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21080 Accepted Submission(s): 6449

5 6 0 0
7 4 7 8 7 6 0 0
5 3 5 6 5 2 0 0
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
int father[maxn+],rank[maxn+];
struct nod
{
nod(int m,int n):a(m),b(n){};
int a,b;
};
void init(int n)
{
for(int i=;i<=n;i++)
{
father[i]=i;
rank[i]=;
}
}
int setfind(int x)
{
/*如果root不为自己,那就去找最原始的root*/
if(x!=father[x])
{
father[x]=setfind(father[x]);
}
return father[x];
}
void collect(int x, int y)
{
x=setfind(x);
y=setfind(y);
if(x!=y)
{
if(rank[x]>rank[y])
{
father[y]=x;
rank[x]+=rank[y];
}
else
{
father[x]=y;
rank[y]+=rank[x];
}
}
} int main()
{
int a,b,max,i;
/*freopen("test.in","r",stdin);*/
while()
{
vector<nod>sa;
max=;
scanf("%d%d",&a,&b);
if(a==-&&b==-)
return ;
while(a+b)
{
int temp=a>b?a:b;
if(max<temp)
max=temp;
nod str(a,b);
sa.push_back(str);
scanf("%d%d",&a,&b);
}
int len=sa.size();
if(len==)
{
puts("Yes");
continue;
}
init(max);
vector<nod>::iterator it;
for(it=sa.begin();it!=sa.end();it++)
{
/*printf("%d%d",(*it).a,it->b);*/
collect((*it).a,it->b);
}
int anst=,pos,ansa=; for( i=;i<=max;i++)
{
/* printf("%d",father[i]);*/
if(anst<rank[i])
{
anst=rank[i];
pos=i; /*确定root*/
}
if(father[i]!=i)
ansa++;
}
if(anst==ansa+) /*是一棵树*/
{
bool tag=true;
for(i=;i<=max;i++)
{
/*是否有循环的树*/
if(father[i]!=pos&&father[i]!=i)
{
tag=false;
break;
}
}
if(tag)
printf("No\n");
else
printf("Yes\n");
}
else
printf("No\n");
}
return ;
}
HDUOJ------(1272)小希的迷宫的更多相关文章
- HDU 1272小希的迷宫(裸并查集,要判断是否构成环,是否是连通图)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1272 小希的迷宫(并查集+最小生成树+队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1272 小希的迷宫 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 第二条并查集,和畅通工程的解法类似.判断小希的迷宫不符合条件,即有回路.我的做法是,在合并两个集 ...
- hdu 1272 小希的迷宫(java实现)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 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 ...
- HDU 1272 小希的迷宫 (并查集)
小希的迷宫 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/L Description 我们的小伙伴Bingo身为大二学长,他乐于 ...
- HDU 1272 小希的迷宫(乱搞||并查集)
小希的迷宫 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有 ...
- hdu 1272 小希的迷宫【并查集】
<题目链接> 小希的迷宫 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的 ...
- hdoj 1272 小希的迷宫 又一个并查集的简单应用
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
随机推荐
- 《Windows核心编程》第十一章——线程池
隐式使用工作项 #include <iostream> #include <windows.h> ; VOID NTAPI SimpleCallback(PTP_CALLBAC ...
- 使用Scala
1. 净资产应用实例 我们要构建这样一个应用,它会取回一份列表,其中包括用户持有的股票的代码以及股份,并告知他们在当前日期为止的这些投资的总价.这包含了几件事:获取用户输入.读文件.解析数据.写文件. ...
- WhyGL:一套学习OpenGL的框架,及翻写Nehe的OpenGL教程
最近在重学OpenGL,之所以说重学是因为上次接触OpenGL还是在学校里,工作之后就一直在搞D3D,一转眼已经毕业6年了.OpenGL这门手艺早就完全荒废了,现在只能是重学.学习程序最有效的办法是动 ...
- Android高效加载大图、多图解决方案,有效避免程序内存溢出现象
好久没有写博客了,今天就先写一个小的关于在Android中加载大图如何避免内存溢出的问题. 后面会写如何使用缓存技术的核心类,android.support.v4.util.LruCache来加载图片 ...
- 解决 ECSHOP v273 产品详情页面评论不显示的问题
问题描述: 最近同事使用ECSHOP v273帮客户开发了一个商城系统,部署到服务器在测试的时候发现产品详情页面里测试的评论不显示,只显示了数量(其实是产品购买的次数) 因为同事搞了好长时间都 ...
- isPostback 的原理及作用(很easy)
1.IsPostBack用来推断表单是否是回发. (不是第一次请求),是点击表单的提交button回发过来的.是否是回发与get请求还是Post请求无关.可是普通情况下回发都是Post请求. 一般Ge ...
- [Node.js]30. Level 6: Listen 'Question' from client, and then Answer the Question
Clients can also answer each other questions, so let's build that feature by first listening for the ...
- Tomcat环境的搭建(web基础学习笔记一)
一.下载和安装Tomcat服务器 下载Tomcat安装程序包:http://tomcat.apache.org/ 点击[Download]跳转到如下图所示的下载页面 二.点击左侧要下载的版本,选择To ...
- 如何在hosts文件添加自己想要解析的网站?及修改hosts的作用
http://union.zhuna.cn/help/144.asp 在Windows2003/XP系统中位于C:\Winnt\System32\Drivers\Etc 目录中,找到host文件. 首 ...
- 【转】Spring中IoC的优点与缺点
1. 优点 我们知道,在Java基本教程中有一个定律告诉我们:所有的对象都必须创建:或者说:使用对象之前必须创建,但是现在我们可以不必一定遵循这个定律了,我们可以从Ioc容器中直接获得一个对象然后直接 ...