首页 > 其他分享 >HDU 1272 小希的迷宫

HDU 1272 小希的迷宫

时间:2022-11-09 19:07:37浏览次数:66  
标签:HDU 小希 get int 1272 ins fa maxn include


Problem Description


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



Input


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


Output


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


Sample Input


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


Sample Output


YesYes

No


判断一个图是不是一棵树。并查集的简单应用,不过要用非递归的,不会有爆栈的风险。

#include<stack>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
const int maxn = 100005;
int ins[maxn], fa[maxn], x, y, z;
bool f;

int get(int x)
{
int y = x,t;
while (x != fa[x]) x = fa[x];
while (fa[y] != x) { t=y; y=fa[y]; fa[t] = x; }
return x;
}

void begin()
{
for (int i = 1; i < maxn - 4; i++) fa[i] = i;
memset(ins, 0, sizeof(ins)); f = true;
}

int main()
{
begin();
while (~scanf("%d%d", &x, &y))
{
if (x == -1 &&y == -1) break;
if (x == 0 && y == 0)
{
for (int i = 1; i < maxn - 4; i++)
if (ins[i] && get(i) != get(z)) f = false;
printf("%s\n", f ? "Yes" : "No");
begin(); continue;
}
ins[x] = ins[y] = 1;
x = get(x); y = get(y); z = x;
if (x != y) fa[x] = y; else f = false;
}
return 0;
}



标签:HDU,小希,get,int,1272,ins,fa,maxn,include
From: https://blog.51cto.com/u_15870896/5838279

相关文章

  • HDU 2242 考研路茫茫——空调教室
    ProblemDescription众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们。Lele也是其中一个。而某教室旁边又摆着两个未装上的空调,更是引起人们无......
  • HDU 4041 Eliminate Witches!
    ProblemDescriptionKanameMadokaisaMagicalGirl(MahouShoujo/PuellaMagi).ThedutyofaMagicalGirlistoeliminateWitches(Majo).Thoughsoundshorrif......
  • HDU 1237 简单计算器
    ProblemDescription读入一个只包含+,-,*,/的非负整数计算表达式,计算该表达式的值。Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字......
  • HDU 4739 Zhuge Liang's Mines
    ProblemDescriptionIntheancientthreekingdomperiod,ZhugeLiangwasthemostfamousandsmartestmilitaryleader.HisenemywasShimaYi,whoalways......
  • HDU 3608 最长回文
    ProblemDescription给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba,abba等 Inp......
  • HDU 5591 ZYB's Game
    ProblemDescription withhisclassmatesinhiking:ahostkeepsanumberin  loses,orthehostwilltellalltheplayersthatthenumbernowisbigger......
  • HDU 4433 locker
    ProblemDescriptionApasswordlockerwithNdigits,eachdigitcanberotatedto0-9circularly.Youcanrotate1-3consecutivedigitsupordownino......
  • HDU 5586 Sum
    ProblemDescriptionThereisanumbersequence ,youcanselectainterval[l,r]ornot,allthenumbers  willbecome ..Afterthat,thesumofnnumbers......
  • HDU 4436 str2int
    ProblemDescriptionInthisproblem,youaregivenseveralstringsthatcontainonlydigitsfrom'0'to'9',inclusive.Anexampleisshownbelow.1......
  • HDU 5264 pog loves szh I
    ProblemDescriptionPoghaslotsofstrings.Andhealwaysmixestwoequal-lengthstrings.Forexample,therearetwostrings:"abcd"and"efgh".Afterm......