歐拉道路:
從無向圖中的一個節點出發走一條道路,每條邊恰好經過一次,這樣的線路成為歐拉道路。
下面給出歐拉道路的判定方法:
有向圖:
圖必須是連通的,而且最多只能有兩個點入度不等於出度,而且這兩個點其中一個點的入度+1=出度,另一個點的出度+1=入度,如果有的點出度!=入度&&出度與入度的絕對值差還不等於1,則這個圖不是歐拉道路。
無向圖:
圖必須是連通的,而且最多有兩個奇度點,則是歐拉道路。
判定圖連通的方法:
無向圖用dfs訪問,看看點是否全部被訪問。
有向圖先轉化為無向圖,然后再用dfs判定。
歐拉回路:
如果一個回路是歐拉路徑,則稱為歐拉回路。
下面給出歐拉回路的判定方法:
有向圖:
圖連通,且所有頂點的入度等於出度。
無向圖:
圖連通,且沒有奇度點。
下面給出一個歐拉道路的例題:
題目為UVa的10129:
題目:
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.
There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
題目大意翻譯:
有一些秘密的門包含着非常有趣的單詞迷題, 考古學家隊伍必須解決它們才能夠打開大門。 因為沒有其他方法能偶打開這些門, 所以解決那些迷題對我們非常重要。
在每個門上有很多個有磁力的盤子,盤子上面寫着單詞。 必須重新移動放置這些盤子,讓它們形成一個隊列:隊列中,除了第一個單詞,每個單詞的開頭和上一個單詞的結尾字母
一樣。例如, motorola的后面可以接上acm。
你的任務是寫一個程序, 讀入一系列單詞,然后計算確定它們是否有可能被排成這樣的隊列。
樣例輸入:
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
樣例輸出:
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
思路:
可以把首尾字母看做節點,把單詞看成有向邊,構造有向圖,如果有解,當且僅當圖中有歐拉路徑。
上面說到了有向圖的歐拉路徑的判定方法:1.將有向圖轉化為無向圖判定連通,2.看入度和出度的關系。