hdu 1272 小希的迷宫(java实现)
小希的迷宫
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19599 Accepted Submission(s): 5993

整个文件以两个-1结尾。
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
Yes
No
import java.util.HashSet;
import java.util.Scanner; public class Main {//先判断是否为连通图,然后是删边问题,M-(n-1)==0
private static int str[];
private static boolean s[];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
HashSet<Integer> Hs=new HashSet<Integer>();
int m=0;
str = new int[100001];
s=new boolean[100001];//标记这个顶点是否出现过
for (int i = 1; i <= 100000; i++)
str[i] = i;
int a = input.nextInt();
int b = input.nextInt();
if(a==0&&b==0){//0 0情况是一个点也没有,也属于满足条件
System.out.println("Yes");
continue;
}
if (a == -1 && b == -1)
break;
while (!(a == 0 && b == 0)) {
//记录边的条数
m++;
//记录顶点个数
Hs.add(a);
Hs.add(b);
s[a]=true;
s[b]=true;
heBing(a, b);//并查集,合并区间
a = input.nextInt();
b = input.nextInt();
}
int sum=0;
for (int i = 1; i <= 100000; i++)//判断有多少区间
if(s[i]&&str[i] == i)
sum++;
if(sum==1&&(m-Hs.size()==-1)){//一个区间,且边减顶点等于-1,则最优
System.out.println("Yes");
}
else
System.out.println("No");
}
} private static void heBing(int x, int y) {
int x1 = find(x);
int y1 = find(y);
if (x1 != y1)
str[x1] = y1;
} private static int find(int x) {
while (str[x] != x) {
x = str[x];
}
return x;
}
}
//8833413 2013-08-05 12:43:52 Accepted 1272 687MS 6996K 1511 B Java zhangyi
import java.util.Scanner; public class Main {//优化过后
private static int str[];
private static boolean ok = true;
private static boolean s[];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
ok = true;//记录是否有相同的父节点
str = new int[100001];
s=new boolean[100001];
for (int i = 1; i <= 100000; i++)
str[i] = i;
int a = input.nextInt();
int b = input.nextInt();
if(a==0&&b==0){//特殊情况
System.out.println("Yes");
continue;
}
if (a == -1 && b == -1)
break;
while (!(a == 0 && b == 0)) {
//找出最大顶点和最小顶点
if(a>max)
max=a;
if(b>max)
max=b;
if(a<min)
min=a;
if(b<min)
min=b;
//标记顶点是否出现过
s[a]=true;
s[b]=true;
heBing(a, b);//并查集,合并区间
a = input.nextInt();
b = input.nextInt();
}
int sum=0;
for (int i = min; i <= max; i++)//c查找区间个数
if(s[i]&&str[i] == i)
sum++;
if (sum==1&&ok)//如果一个区间,且每个顶点没有重复父节点(边)
System.out.println("Yes");
else
System.out.println("No");
}
} private static void heBing(int x, int y) {
int x1[]=new int[2];
int y1[]=new int[2];
x1=find(x);
y1=find(y);
if(x1[0]!=y1[0])
//深度小的合并到深度大的上边
if(x1[1]<y1[1]){
str[x1[0]]=y1[0];
//把此节点也连到父节点上
str[x]=y1[0];
}
else{
str[y1[0]]=x1[0];
str[y]=x1[0];
}
else//说明有相同父节点,即有连通图
ok=false;
}
private static int[] find(int x) {
int b[]=new int[2];
b[0]=x;b[1]=0;
while(str[b[0]]!=b[0]){
b[0]=str[b[0]];
b[1]++;//记录深度
}
return b;
}
}
import java.util.HashSet;
import java.util.Scanner;
public class Main{//AC 原来系统保证图是连通图
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while(true){
HashSet<Integer> HS=new HashSet<Integer>();
int m=0;
int a=input.nextInt();
int b=input.nextInt();
if(a==-1&&b==-1)
break;
while(!(a==0&&b==0)){
HS.add(a);
HS.add(b);
m++;
a=input.nextInt();
b=input.nextInt(); }
// System.out.println(m+"-----"+HS.size());
if(m==0||m-(HS.size()-1)==0)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
hdu 1272 小希的迷宫(java实现)的更多相关文章
- 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 小希的迷宫 并查集
小希的迷宫 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小希的迷宫(并查集+拓扑排序)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 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来走.但是她设计迷宫的 ...
随机推荐
- 理解一条语句:SELECT difference(sum("value")) FROM "mq_enqueue" WHERE "channel" =~ /ActiveMQ_TEST/ AND $timeFilter GROUP BY time($interval)
最近使用grafana在查询InfluxDB中,用到了这一条语句 SELECT difference(sum("value")) FROM "mq_enqueue&quo ...
- Learning a Deep Compact Image Representation for Visual Tracking
这篇博客对论文进行了部分翻译http://blog.csdn.net/vintage_1/article/details/19546953,不过个人觉得博主有些理解有误. 这篇博客简单分析了代码htt ...
- Codeforces 351D Jeff and Removing Periods(莫队+区间等差数列更新)
题目链接:http://codeforces.com/problemset/problem/351/D 题目大意:有n个数,每次可以删除掉数值相同并且所在位置成等差数列的数(只删2个数或者只删1个数应 ...
- Mybatis的核心配置
之前了解了Mybatis的基本用法,现在学习一下Mybatis框架中的核心对象以及映射文件和配置文件,来深入的了解这个框架. 1.Mybatis的核心对象 使用MyBatis框架时,主要涉及两个核心对 ...
- vue 子父组件之间的通信,及在调用组件的地方
这里是用了 element ui 你们也可以看一下管方的文档 http://element.eleme.io/#/zh-CN/component/installation 组件html <div ...
- 学习build-web-application-with-golang第四章内容
GITHUB网址: https://github.com/astaxie/build-web-application-with-golang 内容 4.表单 4.1 处理表单的输入 4.2 验证表单的 ...
- GUC-5 CountDownLatch闭锁
/* * CountDownLatch :闭锁,在完成某些运算是,只有其他所有线程的运算全部完成,当前运算才继续执行 */ public class TestCountDownLatch { publ ...
- linux中忘记root密码解决方案
方法一: 如果用户具有sudo权限,那么直接可以运行如下命令: #sudo su root #passwd #更改密码 或者直接运行sudo passwd root命令就可以直接更改root密码. 有 ...
- EditText属性描述
android:layout_gravity="center_vertical"//设置控件显示的位置:默认top,这里居中显示,还有bottom android:hint=&qu ...
- ThinkPHP3.2.3 PHPExcel读取excel插入数据库
版本 ThinkPHP3.2.3 下载PHPExcel 将这两个文件放到并更改名字 excel文件: 数据库表: CREATE TABLE `sh_name` ( `name` varchar(255 ...