仍旧裸敲并查集。有这两点注意:

1.输入 0 0 时候要输出YES

2.留心数组的初始化

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <numeric>
#include <algorithm>
#include <cctype>
#include <string> using namespace std; const int MAXN = ; bool flag[MAXN];
int connectedList[MAXN];
bool isOk = true;
int nStart, nEnd; void Init() {
for (int i = ; i < MAXN; ++i) {
connectedList[i] = i;
flag[i] = false;
}
} int getFather(int vetex) {
int pos = vetex;
while(pos != connectedList[pos]) {
pos = connectedList[pos];
}
return pos;
} int countListNum() {
int count = ;
for (int i = ; i < MAXN; ++i) {
if (flag[i] == true) {
if (getFather(i) == i) {
++count;
}
}
}
return count;
} void process() {
if( getFather(nStart) == getFather(nEnd)) {
isOk = false;
return;
}
if( getFather(nStart) == nStart && getFather(nEnd) == nEnd) {
connectedList[nEnd] = nStart;
} else if(getFather(nStart) == nStart) {
connectedList[nStart] = getFather(nEnd);
} else {
connectedList[nEnd] = getFather(nStart);
}
flag[nStart] = true;
flag[nEnd] = true;
}
int main() {
while(cin >> nStart >> nEnd) {
if(nStart == - && nEnd == -) break;
if(nStart || nEnd) {
isOk = true;
memset(flag, false , sizeof(flag));
Init();
process(); while(cin >> nStart >> nEnd, nStart || nEnd) {
if(isOk == true) {
process();
}
} if(isOk == true) {
int count = ;
count = countListNum();
if(count > ) {
isOk = false;
}
}
} else {
isOk = true;
}
if(isOk == false) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
}
return ;
}

【HDU1272】小希的迷宫(并查集基础题)的更多相关文章

  1. hdu1272 小希的迷宫(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目: 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) ...

  2. HDU1272小希的迷宫–并查集

    上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...

  3. hdu-1272 小希的迷宫---并查集或者DFS

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目大意: Problem Description 上次Gardon的迷宫城堡小希玩了很久(见 ...

  4. HDU-1272小希的迷宫,并查集?其实不用并查集;

    小希的迷宫                                                                                               ...

  5. HDU1272 小希的迷宫 并查集

    参考网址:http://blog.sina.com.cn/s/blog_6827ac4a0100nyjy.html 解题思路: 由于这里出现的数字不一定连续的数字都会出现,所以设一个mark来标记数字 ...

  6. hdu1272小希的迷宫(并查集判断回路和是否连通)

    传送门 迷宫中不能有回路,还要连通 如果最后集合数是一个那就是连通,否则不联通 要合并的两个顶点在相同集合内,表示出现了回路 输入时注意一下 #include<bits/stdc++.h> ...

  7. HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质

    小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...

  8. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  9. HDU 1272 小希的迷宫 并查集

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

随机推荐

  1. Mybatis 开发中遇见的异常及处理

    1 异常信息: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.Bin ...

  2. OCP-1Z0-051-题目解析-第28题

    28. Which two statements are true regarding constraints? (Choose two.)  A. A foreign key cannot cont ...

  3. jQuery中 $ 符号的冲突问题

    jQuery中 $ 符号的冲突问题是常见问题之一.   在jQuery中,$是jQuery的别名,为了书写方便,我们更习惯用$('#id')这一类的方式来书写代码.当同一页面引用了jQuery多个版本 ...

  4. Android 自定义控件 优雅实现元素间的分割线 (支持3.0以下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42407923 ,本文出自:[张鸿洋的博客] 1.概述 话说,随着Android ...

  5. 代码实现Layout android:layout_alignParentRight

    代码实现Layout android:layout_alignParentRight 例如: android:id="@+id/account_option" android:la ...

  6. JavaScripts学习日记——ECMAscript

    1.Function对象 Function是一个很特殊的对象,特殊在该对象就像java中的方法一样,可以运行,可以传参数. 三种定义function对象的方法: 1.function fun1(a,b ...

  7. 修改EF的默认约定模型的方式

    EF默认使用约定的模型,但是有时类不遵从约定,我们需要能够执行进一步的配置.对此有两种方法:数据注释和Fluent API 注释 class User { [Key] public int Key{ ...

  8. FineUI布局应用(二)

    一.FineUI页面布局分为 1.Fit布局 <f:Panel ID="Panel1" Title="布局Fit(Layout=Fit)" runat=& ...

  9. Invalid content was found starting with element 'taglib'”

    今天在使用struts-menu制作菜单,在web.xml中写入 <taglib>      <taglib-uri>/WEB-INF/struts-menu.tld</ ...

  10. Objective-C set/get方法

    主要内容set get方法的使用 关键字 @property 全自动生成set get方法 // 类的声名 @interface People : NSObject{ int _age; // 成员变 ...