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

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. 解决selenium 启动ie浏览器报错:Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones

    启动ie代码: System.setProperty("webdriver.ie.driver", "bin/IEDriverServer.exe"); Web ...

  2. c语言 (linux下)

    生成二进制 : gcc -o hello hello.c 生成汇编:gcc -o hello.s -S hello.c 生成预编译文件:gcc -o hello.i -E hello.c int ma ...

  3. python标准库之字符编码详解

    codesc官方地址:https://docs.python.org/2/library/codecs.html 相关帮助:http://www.cnblogs.com/huxi/archive/20 ...

  4. iOS之即时通讯相关理解

    Socket: 1>Socket又称"套接字" 2>网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 3>应用程序通常通 ...

  5. Git冲突解决方案

    Git冲突解决方案 1.  在代码提交时,先更新,若有冲突.先解决冲突.若提交之后在review时才发现无法合并代码时有冲突,需要abandon此次提交的代码. 2.  解决冲突的基本做法,保存本地代 ...

  6. Laravel Eloquent ORM

    Eloquent ORM 简介 基本用法 集体赋值 插入.更新.删除 软删除 时间戳 查询范围 关系 查询关系 预先加载 插入相关模型 触发父模型时间戳 与数据透视表工作 集合 访问器和调整器 日期调 ...

  7. gcc/g++/make 编译信息带颜色输出

    假设编译一个项目错误警告太多.很不好找,所以很希望输出信息能够带有颜色. 但是 gcc 4.9.0 之前的版本号并不支持,非常多情况下是不能替换编译器的,比方使用交叉编译器, 也能够使用 colorg ...

  8. eclipse注释模板修改

    http://swiftlet.net/archives/1199 以下为模板文件 <?xml version="1.0" encoding="UTF-8" ...

  9. Kerberos-KDC

    Kerberos提供一种较好的解决方案,它是由MIT发明的,Kerberos建立了一个安全的.可信任的密钥分发中心(KDC, Key Distribution Center).Kerberos是一种认 ...

  10. tomcat线程数查看

    获取tomcat进程pid ps -ef|grep tomcat 统计该tomcat进程内的线程个数 ps -Lf 29295 |wc -l