Hdoj 1272 xiaoxi's Labyrinth solution report

Source: Internet
Author: User
Question

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1272

Xiaoxi's maze

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 18290 accepted submission (s): 5579


Problem description the last time gardon's Labyrinth Castle was played for a long time (see Problem B). Now she wants to design a maze for gardon. However, she had different ideas for designing the maze. First, she thought that all the channels should be connected in two directions. That is to say, if one channel connects room A and B, it can be used to go from room A to Room B, or from Room B to room A to improve the difficulty, john hopes that any two rooms can have only one path to communicate with each other (unless they go back ). Xiaoxi now gives you her design drawing to help you determine whether her design drawing conforms to her design idea. For example, in the following example, the first two conditions are met, but the last one has two methods from 5 to 8.
 


The input contains multiple groups of data. Each group of data is a list of integer pairs ending with 0, indicating the numbers of the two rooms connected by one channel. The number of the room must be at least 1 and cannot exceed 100000. There is an empty row between each two groups of data.
The entire file ends with two-1 characters.


For each group of input data, output only contains one row. If the maze conforms to Xiao Xi's idea, "yes" is output; otherwise, "no" is output ".


Sample Input

6 8  5 3  5 2  6 45 6  0 08 1  7 3  6 2  8 9  7 57 4  7 8  7 6  0 03 8  6 8  6 45 3  5 6  5 2  0 0-1 -1
 


Sample output

YesYesNo

Analysis

Because of the large amount of data, querying sets is a good solution. If the two nodes are in the same set during union, a loop exists. At the beginning, I forgot one point, that is, the connectivity of the entire graph. I just thought about whether the loop exists. Contributed a wa

Code

Previous wa code:

#include <cstdio>#include <cstring>using namespace std;bool flag[100005], ok;;int p[100005];void init(){    ok = true;    memset(flag, false, sizeof(flag));    for(int i = 0; i < 100005; i ++) p[i] = i;}int Find(int x){    int r = x;    while(r != p[r]) r = p[r];    //compress path    int k = x;    while(k != r)    {        int tmp = p[k];        p[k] = r;        k = tmp;    }    return r;}bool Union(int a, int b){    int ra = Find(a), rb = Find(b);    if(ra == rb) return false;    else p[ra] = rb; //union    return true;}int main(){    init();    int a, b;    while(scanf("%d%d", &a, &b) && !(a == -1 && b == -1))    {        if(!a && !b)        {            if(ok) printf("Yes\n");            else printf("No\n");            init();            continue;        }        if(Union(a, b) == false) ok = false;    }    return 0;}

After the connectivity is added, the AC code is as follows:

#include <cstdio>#include <cstring>using namespace std;bool flag[100005], ok;;int p[100005];void init(){    ok = true;    memset(flag, false, sizeof(flag));    for(int i = 0; i < 100005; i ++) p[i] = i;}int Find(int x){    int r = x;    while(r != p[r]) r = p[r];    //compress path    int k = x;    while(k != r)    {        int tmp = p[k];        p[k] = r;        k = tmp;    }    return r;}bool Union(int a, int b){    int ra = Find(a), rb = Find(b);    if(ra == rb) return false;    else p[ra] = rb; //union    return true;}int main(){    init();    int a, b;    while(scanf("%d%d", &a, &b) && !(a == -1 && b == -1))    {        if(!a && !b)        {            //check connectivity            for(int i = 1, components = 0; i < 100005; i ++) if(flag[i])            {                if(p[i] == i) components ++;                if(components > 1) ok = false; //not connected            }            if(ok) printf("Yes\n");            else printf("No\n");            init();            continue;        }        if(Union(a, b) == false) ok = false;        flag[a] = flag[b] = true;    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.