HDU 1269 Maze Castle (DFS)

Source: Internet
Author: User

Maze Castle Problem description in order to train the sense of direction, Gardon built a large castle with N rooms (n<=10000) and M-Lanes (m<=100000), each one unidirectional, That is, if a channel is said to be connected to room A and room B, only the room B can be reached by this passage, but it does not mean that it can be reached by Room B. Gardon need to ask you to write a procedure to confirm whether any two rooms are interconnected, namely: for arbitrary I and J, there is at least one path can be from room I to room J, there is a path can be from room J to room I.

The input input contains more than one set of data, the first line of inputs has two numbers: N and M, and the next m row has two numbers a and B, indicating that a passage can come from room A to room B. The file ends with two 0. Output for each set of data entered, if any two rooms are connected to each other, outputs "Yes", otherwise output "No". Sample INPUT3 3
1 2
2 3
3 1
7 ·
1 2
2 3
3 2
0 0 Sample Outputyes
No answer violence DFS, starting from the first room search: 1->2->3, because of the existence of the ring, to determine whether to return to the starting point, otherwise it will be infinite loop. A room can go to a room with set to record, not repeat.
#include <Set>#include<cmath>#include<queue>#include<vector>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#defineMS (a) memset (A,0,sizeof (a))#defineMSP Memset (Mp,0,sizeof (MP))#defineMSV memset (vis,0,sizeof (VIS))using namespacestd;intn,m;BOOLvis[10001];Set<int> s[10001];voidDfsintOriintx) {    Set<int>:: Iterator it; Vector<int>v;  for(It=s[x].begin (); It!=s[x].end (); it++) V.push_back (*it); Vector<int>:: iterator ITV;  for(Itv=v.begin (); Itv!=v.end (); itv++)    {        if(Vis[*itv])Continue; //If the room you are searching for is smaller than the starting room number, it means that the room has been searched.//at this point, just insert all the rooms that you can walk into .        /*//This pruning has a problem if (*itv<ori) {for (It=s[*itv].begin (); It!=s[*itv].end (); it++) {                if (*it==ori) continue;            S[ori].insert (*it);        } continue; }        */S[ori].insert (*ITV); vis[*itv]=1; DFS (Ori,*ITV); }}intMain () { while(cin>>n>>m&& (n| |m)) { while(m--)        {            intb; CIN>>a>>b;        S[a].insert (b); }        BOOLflag=1;  for(intI=1; i<=n; i++)        {            if((int) s[i].size () ==n-1)Continue;            msv Vis[i]=1;            DFS (i,i); //pruning, if a room cannot walk to all the other rooms,//There's no further search .            if((int) s[i].size ()!=n-1) {flag=0;  Break; }        }         for(intI=1; i<=n&&flag;i++)        {            if((int) s[i].size ()!=n-1) {flag=0; }        }        if(flag) printf ("yes\n"); Elseprintf"no\n");  for(intI=1; i<=n;i++) s[i].clear (); }    return 0;}
View Code

HDU 1269 Maze Castle (DFS)

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.