POJ 1300 Door Man(欧拉回路的判定)
生活随笔
收集整理的這篇文章主要介紹了
POJ 1300 Door Man(欧拉回路的判定)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接
題意 : 莊園有很多房間,編號從0到n-1,能否找到一條路徑經(jīng)過所有開著的門,并且使得通過門之后就把門關(guān)上,關(guān)上的再也不打開,最后能回到編號為0的房間。
思路 : 這就是一個(gè)赤裸裸的判斷歐拉通路的問題了,但實(shí)際上,就只有兩種情況能夠輸出YES,以房間為頂點(diǎn),連接房間之間的門為邊構(gòu)造圖,這兩種情況分別是存在歐拉回路和歐拉通路的情況:所有房間都是偶數(shù)個(gè)門并且起始房間就是0,所以可以回到0,存在歐拉回路;有兩個(gè)房間的門是奇數(shù)個(gè),其余都是偶數(shù)個(gè),這種情況下,要求出發(fā)房間和0房間的門是奇數(shù)個(gè),并且其實(shí)房間不能是0,因?yàn)椴淮嬖?到0的歐拉回路,但是存在別的房間到0的歐拉通路。
1 //POJ 1300 2 #include <stdio.h> 3 #include <string> 4 #include <iostream> 5 #include <string.h> 6 7 using namespace std ; 8 9 int M,N,door[20] ; 10 string sh ; 11 char sh1[789] ; 12 int main() 13 { 14 while(cin >> sh) 15 { 16 if(sh == "ENDOFINPUT") 17 break ; 18 cin >> M >> N ; 19 getchar() ; 20 int cnt = 0 ; 21 memset(door,0,sizeof(door)) ; 22 for(int i = 0 ; i < N ; i++) 23 { 24 gets(sh1) ; 25 int len = strlen(sh1) ; 26 for(int j = 0 ; j < len ; j++) 27 { 28 if(sh1[j] != ' ') 29 { 30 int d = sh1[j]-'0' ; 31 cnt ++ ; 32 door[i] ++ ; 33 door[d] ++ ; 34 } 35 } 36 } 37 cin >> sh ; 38 int odd = 0 ,even = 0 ; 39 for(int i = 0 ; i < N ; i++) 40 { 41 if(door[i] % 2) odd ++ ; 42 else even ++ ; 43 } 44 if(odd == 0 && M == 0) 45 cout<< "YES "<< cnt <<endl ; 46 else if(odd == 2 && M != 0) 47 cout << "YES "<<cnt <<endl ; 48 else cout<<"NO"<<endl ; 49 } 50 return 0 ; 51 } View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/luyingfeng/p/3877442.html
總結(jié)
以上是生活随笔為你收集整理的POJ 1300 Door Man(欧拉回路的判定)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xxx项目接口文档模板
- 下一篇: 蛋疼的中文编码及其计算机编码历史