题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1272

思路

只需要判断 这张图 无环 并且只有一个连通块 就可以了

要注意 如果 只输入 0 0 那给的是空图 要给出 Yes

判断 无环 有两个方法

0.在并查集的合并操作中 如果 find(x) == find(y) 就说明 x 和 y 已经有一条边了 再加 就是成环了 直接 flag = 0

1.我们可以对边的个数 计数 只有 边数== 结点个数-1 才是满足条件的

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int MOD = 1e9 + 7; int pre[maxn]; int find(int x)
{
int r = x;
while (pre[r] != r)
r = pre[r];
int i = x, j;
while (i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
} void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (x != fy)
pre[fx] = fy;
} void init()
{
for (int i = 0; i < maxn; i++)
pre[i] = i;
} int main()
{
int x, y;
set <int> s;
s.clear();
int edge = 0;
init();
while (scanf("%d%d", &x, &y))
{
if (x == -1 && y == -1)
break;
else if ((x || y))
{
edge++;
s.insert(x);
s.insert(y);
join(x, y);
}
else
{
if (edge == 0)
{
printf("Yes\n");
continue;
}
map <int, int> m;
int len = s.size();
while (!s.empty())
{
int num = find(*s.begin());
s.erase(s.begin());
m[num]++;
}
int flag = 1;
if (edge != len - 1 || m.size() != 1)
flag = 0;
if (flag)
printf("Yes\n");
else
printf("No\n");
s.clear();
edge = 0;
init();
}
}
}

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int MOD = 1e9 + 7; int pre[maxn]; int flag; int find(int x) //路径压缩
{
int r = x;
while (pre[r] != r)
r = pre[r];
int i = x, j;
while (i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
} //int find(int x)
//{
// while (x != pre[x])
// x = pre[x];
// return x;
//} void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)
pre[fx] = fy;
else
flag = 0;
} void init()
{
for (int i = 0; i < maxn; i++)
pre[i] = i;
} int main()
{
int x, y;
set <int> s;
s.clear();
init();
flag = 1;
while (scanf("%d%d", &x, &y))
{
if (x == -1 && y == -1)
break;
else if ((x || y))
{
s.insert(x);
s.insert(y);
join(x, y);
}
else
{
if (s.size() == 0)
{
printf("Yes\n");
continue;
}
map <int, int> m;
int len = s.size();
while (!s.empty())
{
int num = find(*s.begin());
s.erase(s.begin());
m[num]++;
}
if (m.size() != 1)
flag = 0;
if (flag)
printf("Yes\n");
else
printf("No\n");
s.clear();
init();
flag = 1;
}
}
}

HDU - 1272 小希的迷宫 【并查集】的更多相关文章

  1. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

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

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

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

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

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

  4. (step5.1.6)hdu 1272(小希的迷宫——并查集)

    题目大意:输入一系列的点,判断这些点组成的图符不符合小希的思路(无环.连通) 解题思路: 1)如果两个节点的根节点相同,那么在这两个节点之间添加1条边以后,这个图肯定有环路. 2)孤立节点:被使用过& ...

  5. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  6. HDU 1272小希的迷宫(裸并查集,要判断是否构成环,是否是连通图)

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

  7. hdu 1272 小希的迷宫(并查集+最小生成树+队列)

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

  8. HDU 1272 小希的迷宫 (并查集)

    小希的迷宫 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/L Description 我们的小伙伴Bingo身为大二学长,他乐于 ...

  9. HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏

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

随机推荐

  1. Java 过滤所有html标签,复制文件到指定位置

    public static String filterHtml(String string) { String str = string.replaceAll("<[a-zA-Z]+[ ...

  2. spring mvc controller中的异常封装

    http://abc08010051.iteye.com/blog/2031992 一直以来都在用spring mvc做mvc框架,我使用的不是基于注解的,还是使用的基于xml的,在controlle ...

  3. delphi的字节对齐

    如果不改编译选项Delphi的Record默认也是4字节对齐的. 可以用编译开关指定 {$A4+}就是4字节对齐.同理{$A2+}.{$A1+}等.{$A1+}等同于Packed Record 主要容 ...

  4. struts2在项目中的应用之下载

    文件下载是一个非经常见的功能,用struts2实现文件下载的步骤: 一)定义一个Action类.FileDownload.java package com.struts2.filedownload; ...

  5. static 修饰的变量在程序中容易出现的问题

    package lianxi; public class StaticTest {    int a = 0;    static int b =0;    StaticTest(){         ...

  6. instagram架构分析_转

    转自:http://www.eit.name/blog/read.php?504 Instagram 团队上个月才迎来第 7 名员工,是的,7个人的团队.作为 iPhone 上最火爆的图片类工具,in ...

  7. 【Python + Selenium】之unittest测试用例满足条件,进行跳过测试Skip

    直接上代码: __author__ = 'zc' import unittest class demoSkipTest(unittest.TestCase): a = 70 b = 50 print( ...

  8. PHPMailer 使用 中文乱码

    WordPress在用. You'll find plenty more to play with in the examples folder. 中文乱码问题: $mail->CharSet ...

  9. 使用 cacti 批量监控服务器以及其 PHP 运作环境配置

    http://www.ibm.com/developerworks/cn/linux/l-cn-cacti/ http://www.360doc.com/content/12/0711/22/1465 ...

  10. 浅谈Java数据结构和算法

    今天的突然看集合底层的时候发现了好多算法和数据结构.再次就比较一下和汇总一下. 数据结构分类:线性结构和非线性结构 问题一: 什么是线性和非线性: 我个人的理解是:数据结构中线性结构指的是数据元素之间 ...