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

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. c语言筛选质数

    #include <stdio.h> #include <stdlib.h> #include <math.h> int isit(int num) { int i ...

  2. PHP数据结构:栈、队列、堆、固定数组

    数据结构:栈 队列: 堆: 固定尺寸的数组:

  3. AndroidStudio常见提示

    Required:请求的是String字符串 .     Found:   et.getText()返回的是text.Editable

  4. MyBatis3整合Spring3、SpringMVC3

    开发环境: System:Windows WebBrowser:IE6+.Firefox3+ JavaEE Server:tomcat5.0.2.8.tomcat6 IDE:eclipse.MyEcl ...

  5. spring 中StoredProcedure的使用方法

    StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate运行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...

  6. Linux查看网络即时网速

    sar -n DEV 1 100 1代表一秒统计并显示一次 100代表统计一百次 使用ntop等工具,就更方便简单了,不过这个更灵活 P.S. sar在sysstat包 来源:http://www.c ...

  7. mysql常用操作(转自阿铭linux)

    在前面两个章节中已经介绍过MySQL的安装了,但是光会安装还不够,您还需要会一些基本的相关操作.当然了,关于MySQL的内容也是非常多的,只不过对于linux系统管理员来讲,一些基本的操作已经可以应付 ...

  8. C#遍历Object各个属性含List泛型嵌套。

    同事遇到一个问题:在做手机app接口时,返回JSON格式,json里面的数据属性均是string类型,但不能出现NULL(手机端那边说处理很麻烦,哎).Model已经创建好了,而且model的每个属性 ...

  9. CodeSmith使用总结--读取一个表试试

    我感觉CodeSmith对于我的最大用途是不用我手动创建每个表的Model.BLL和DAL了,那些繁琐的工作真的让我很无语. CodeSmith要读取数据库中的表就要先连接数据库. 新建一个数据库连接 ...

  10. WebDAV被启用(转)

    WebDAV (Web-based Distributed Authoring and Versioning) 一种基于 HTTP 1.1协议的通信协议.它扩展了HTTP 1.1,在GET.POST. ...