HDU 1272 The maze of little Nozomi (and check the set, judging whether the ring)

Source: Internet
Author: User

The maze of XiaoxiTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 41583 Accepted Submission (s): 12822


Problem description Last Gardon Maze Castle Little Nozomi played for a long time (see Problem B), now she also want to design a maze let Gardon to go. But she designed the maze of different ideas, first she thought all the channels should be two-way connectivity, that is, if there is a channel connected to room A and B, then can go through it from room A to room B, but also through it from Room B to room A, in order to improve the difficulty, Xiaoxi hope that any two rooms have and only one path can be connected (unless you go back). Xiao-Nozomi now gives you her design to help you determine whether her design is in line with her design ideas. For example, the first two are eligible, but the last one has two methods of reaching 8 from 5.


Input inputs contain multiple sets of data, each of which is a list of integer pairs ending in 0 0, representing the number of two rooms to which a channel is connected. The number of the room is at least 1 and no more than 100000. There is a blank line between each of the two sets of data.
The entire file ends with two-1.

Output contains only one row for each set of data that is entered. If the maze conforms to Xiaoxi's idea, then output "Yes", otherwise output "No".

Sample Input
6 8  5 3 5 2 6 6 0 1 7 3  6 2  8 9  7  4 7 8 7 6 0 8 6 8 6 4 5 3  5 6  5 2  0 0-1-1

Sample Output
Yesyesno

Authorgardon
Source

HDU 2006-4 Programming Contest



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

Test instructions: Or check set, the question is whether it is a connected sub-block, whether the formation of a ring. But I have a problem with the input, always wrong ...


Compared to several code, found to be similar, but the running time is very different, it should be the difference between CIN and scanf!


AC Code: 296MS

#include <iostream> #include <cstring>using namespace std;const int Maxn=100000+5;int Fa[maxn];bool vis[        Maxn];bool flag;void Init () {for (int i=1; i<maxn; i++) {vis[i]=false;    Fa[i]=i;    }}int Find (int x) {if (x==fa[x]) return x; Return Fa[x]=find (Fa[x]);}    void Bind (int x,int y) {int fx=find (x);    int Fy=find (y);    if (FX > FY) fa[fx] = FY; else fa[fy] = FX;}    int main () {int a A, B;        while (cin>>a>>b,a!=-1&&b!=-1) {init ();        Flag=false; while (a| |            b) {if (Find (a) ==find (b)) flag=true;            Bind (A, b);            Vis[a]=vis[b]=true;        cin>>a>>b;        } if (flag) cout<< "No" <<endl;            else {int sum=0;            for (int i=1;i<maxn;i++) {if (vis[i]&&fa[i]==i) sum++; }//cout<< "Sum=" <<sum<<endl;            if (sum>1) cout<< "No" <<endl;        else cout<< "Yes" <<endl; }} return 0;}


Q Giant's Code: 62MS

Qscqesze#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include < iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack>typedef Long Long ll;using namespace Std;//freopen ("D.in", "R", stdin);//freopen ("D.out", "w", stdout); #define Sspeed ios_base:: Sync_with_stdio (0); Cin.tie (0) #define Test freopen ("Test.txt", "R", stdin) #define MAXN 200001#define MoD 10007#define EPS 1e-9int num;char ch[20];const int inf=0x3f3f3f3f;const ll INFLL = 0x3f3f3f3f3f3f3f3fll;inline ll read () {ll x=0,f=1    ; Char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;}    inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;}    while (x>0) ch[++num]=x%10,x/=10; while (Num) Putchar (ch[num--]+48); Puts ("");}   int FA[MAXN];    BOOL MARK[MAXN];    int fi (int x) {int t=x;    while (fa[t]!=t) t=fa[t];    return t;        } bool Merge (int x,int y) {int fx=fi (x), Fy=fi (y);          if (fx!=fy) {fa[fx]=fy;      return true;  } return false;    } int main () {//test;      int a,b,i,flag,cnt;           while (scanf ("%d%d", &a,&b) && (a!=-1 && b!=-1)) {flag=1;cnt=0;              if (a==0 && b==0) {printf ("yes\n");          Continue          } for (i=0;i<100010;i++) {fa[i]=i;mark[i]=0; } while (a| |             b) {mark[a]=1;mark[b]=1;             if (merge (b) ==false) flag=0;          scanf ("%d%d", &a,&b);         } if (flag==0) printf ("no\n"); else {for (i=0;i<100010;i++) if (Mark[i] && fa[i]==i) cnt++;              if (cnt==1) printf ("yes\n");          else printf ("no\n");  }} return 0; }

62MS

Flag[i] Whether the array tag I appears, whether the flag tag has a ring, sum records the number of collections #include<stdio.h>const int N = 100005;int Flag[n], father[n];void Init ( {for (int i = 0; I <= 100000; i++) Flag[i] = 0, father[i] = i;}    int find (int x) {if (x = Father[x]) father[x] = find (father[x]); return father[x];}    void Merge (int a, int b) {int p = Find (a);    int q = Find (b); FATHER[P] = q;}    int main () {int A, B;        while (~SCANF ("%d%d", &a,&b)) {if (a = =-1 && b = =-1) break;        Init ();        int FLAG = 0;            while (1) {if (a = = 0 && b = = 0) break;            if (find (a) = = find (b)) FLAG = 1;            Merge (A, b);            Flag[a] = 1, flag[b] = 1;        scanf ("%d%d", &a,&b);        } if (FLAG = = 1) printf ("no\n");            else {int sum = 0;                  for (int i = 0; I <= 100000; i++) if (Flag[i] && father[i] = = i)  sum++;            printf ("%d\n", sum);            if (Sum > 1) printf ("no\n");        else printf ("yes\n"); }} return 0;}


This blog link: http://blog.csdn.net/hurmishine/article/details/51815957

HDU 1272 The maze of little Nozomi (and check the set, judging whether the ring)

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.