【题目】洛谷P2307 迷宫

【题目】洛谷P2307 迷宫

题目描述

小希非常喜欢玩迷宫游戏,现在她自己设计了一个迷宫游戏。在她设计的迷宫中,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。

输入输出格式

输入格式:

输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。
整个文件以两个-1结尾。

输出格式:

对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出”1”,否则输出”0”。

输入输出样例

输入样例#1

6 8  5 3  5 2  6 4
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

输出样例#1

1
1
0

思路

由于是无向连通图,所以可以用并查集解决。
这道题主要抓住两个要点:
1.任意两个房间有且仅有一条路径可以相通.
2.任意两个房间有且仅有一条路径可以相通.
解决方法:
1.输入A,B两个联通点时利用并查集操作把他们并入一个集合中,若在并入前A,B两点已经在同一集合,则表示这两个点已经被其他点联通,不符合题意。
2.读到 0 0 准备退出本次操作时,检查每一个点所在的集合,若存在有两个点存在于不同的集合中,表示这一张图并没有联通,不符合题意。
详情请见代码。

代码如下

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#define by Mashiro_ylb
#define TIME 2017/10/28
using namespace std;
const int maxn_data = 100007;

int father[maxn_data];  //并查集代表数组
int rank[maxn_data]; //统计集合元素数(用于并查集启发式合并)

template<class T>void read(T &x) //读入优化
{
    int f = 0; x = 0; char ch = getchar();
    while(ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
    while(ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    x = f? -x : x;
}
template<class T>void write(T x)  //输出优化
{
    if(x < 0) x = -x, putchar('-');
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
template<class T>T Max(T x, T y){return x > y? x : y;}
template<class T>T Min(T x, T y){return x < y? x : y;}
int find(int x)  //并查集:查询代表元素+路径压缩
{
    int k = x, j;
    while(x != father[x]) x = father[x];
    while(k != x) j = father[k], father[k] = x, k = j;
    return x;
}
void unionn(int x, int y)  //并查集:启发式合并(小集合并入大集合)
{
    x = find(x);
    y = find(y);
    if(rank[x] > rank[y]) father[y] = x, rank[x] += rank[y], rank[y] = 0;
    else father[x] = y, rank[y] += rank[x], rank[x] = 0;
}
bool check(int x, int y){return find(x) == find(y);}  //并查集:查询两元素是否属于同一集合

int init();

int main()
{
//  freopen("in.txt","r",stdin);
    while("Mashiro_ylb") write(init()), putchar(10);  //操作
    return 0;
}

int init()
{
    int maxx = 0, minn = 0x7fffffff;
    int flag = 1;
    int x, y;
    memset(rank, 0, sizeof(rank));  //并查集初始化
    memset(father, 0, sizeof(father));
    read(x);read(y);
    while(x > 0 && y > 0)
      {
        if(!father[x]) father[x] = x;  //并查集初始化
        if(!father[y]) father[y] = y;
        if(check(x, y)) flag = 0;  //存在多条路径,不合题意
        else unionn(x, y);
        maxx = Max(x, Max(y, maxx));  //查找输入点的最大值与最小值(减小下一步判断图联通的复杂度)
        minn = Min(x, Min(y, minn));
        read(x);read(y);
      }
    if(x == -1 && y == -1) exit(0);  //依题意结束程序
    for(int i = minn + 1; i <= maxx; i++)  //判断图是否能联通
      if(find(i) != 0 && find(i) != find(minn))
        return 0;
    return flag;
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Mashiro_ylb/article/details/78378357
今日推荐