hdu 1272 小希的迷宮(並查集+最小產生樹+隊列)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   java   io   strong   

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1272

小希的迷宮

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26818    Accepted Submission(s): 8273


Problem Description上次Gardon的迷宮城堡小希玩了很久(見Problem B),現在她也想設計一個迷宮讓Gardon來走。但是她設計迷宮的思路不一樣,首先她認為所有的通道都應該是雙向連通的,就是說如果有一個通道連通了房間A和B,那麼既可以通過它從房間A走到房間B,也可以通過它從房間B走到房間A,為了提高難度,小希希望任意兩個房間有且僅有一條路徑可以相通(除非走了回頭路)。小希現在把她的設計圖給你,讓你幫忙判斷她的設計圖是否符合她的設計思路。比如下面的例子,前兩個是合格,但是最後一個卻有兩種方法從5到達8。
 

 

Input輸入包含多組資料,每組資料是一個以0 0結尾的整數對列表,表示了一條通道串連的兩個房間的編號。房間的編號至少為1,且不超過100000。每兩組資料之間有一個空行。
整個檔案以兩個-1結尾。 

 

Output對於輸入的每一組資料,輸出僅包括一行。如果該迷宮符合小希的思路,那麼輸出"Yes",否則輸出"No"。 

 

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

 

Sample OutputYesYesNo  題目大意:簡單的理解就是:(1)不能構成一個圓  (2)要在一棵樹上 這兩個條件必須全都要滿足,缺一不可。這裡我是用隊列來解決是否在一棵樹這個問題的,我先存到隊列裡面,再取出來判斷。還有需要注意的就是當輸入0 0時,輸出的為Yes。 詳見代碼。 
 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6  7 int father[100009]; 8  9 void set()10 {11     for (int i=1; i<100009; i++)12         father[i]=i;13 }14 15 int find(int a)16 {17     while (father[a]!=a)18         a=father[a];19     return a;20 }21 22 int  Union(int x,int y)23 {24     x=find(x);25     y=find(y);26     if (x!=y)27     {28         father[x]=y;29         return 0;30     }31     else32         return 1;33 }34 35 int main ()36 {37     int a,b,k,used[100009];38     while (~scanf("%d%d",&a,&b))39     {40         if (a==0&&b==0)41         {42             printf ("Yes\n");43             continue;44         }45         memset(used,0,sizeof(used));46         queue<int>q;47         q.push(a);48         q.push(b);49         used[a]=1;50         used[b]=1;51 52         int flag=0,cmp=0;53         set();54         if (a==-1&&b==-1)55             break;56         Union(a,b);57         while (scanf("%d%d",&a,&b),a||b)58         {59             if (Union(a,b)==1)60                 flag=1;61             if(used[a]!=1)62             {63                 q.push(a);64                 used[a]=1;65             }66             if (used[b]!=1)67             {68                 q.push(b);69                 used[b]=1;70             }71         }72         while (!q.empty())73         {74             int s=q.front();75             q.pop();76             if (s==father[s])77                 cmp++;78         }79         if (flag==0&&cmp==1)80             printf ("Yes\n");81         else82             printf ("No\n");83     }84     return 0;85 }

 

 

hdu 1272 小希的迷宮(並查集+最小產生樹+隊列)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.