未解決!歐拉通路&&判聯通圖)

題解參考自:https://blog.csdn.net/qq_24489717/article/details/51313618

Problem 2112 Tickets

Accept: 447    Submit: 770
Time Limit: 3000 mSec    Memory Limit : 32768 KB

 Problem Description

You have won a collection of tickets on luxury cruisers. Each ticket can be used only once, but can be used in either direction between the 2 different cities printed on the ticket. Your prize gives you free airfare to any city to start your cruising, and free airfare back home from wherever you finish your cruising.

You love to sail and don't want to waste any of your free tickets. How many additional tickets would you have to buy so that your cruise can use all of your tickets?

Now giving the free tickets you have won. Please compute the smallest number of additional tickets that can be purchased to allow you to use all of your free tickets.

 Input

There is one integer T (T≤100) in the first line of the input.

Then T cases, for any case, the first line contains 2 integers n, m (1≤n, m≤100,000). n indicates the identifier of the cities are between 1 and n, inclusive. m indicates the tickets you have won.

Then following m lines, each line contains two integers u and v (1≤u, v≤n), indicates the 2 cities printed on your tickets, respectively.

 Output

For each test case, output an integer in a single line, indicates the smallest number of additional tickets you need to buy.

 Sample Input

35 31 31 24 56 51 31 21 61 51 43 21 21 2

 Sample Output

120

 Source

“高教社杯”第三屆福建省大學生程序設計競賽



題解:這一題的難點在於判斷圖的每個聯通分塊裏有多少條路構成不了歐拉路,那麼需要根據歐拉路的性質可知,對於無向圖,構成歐拉路的奇數點的個數爲0或者爲2,那麼我們只要統計出這個聯通分塊的(奇數點的個數-2)就是多餘出來的那些點,那麼直接累加(奇數點的個數-2)/2即可,代碼裏有更詳細的說明。


這裏我們使用並查集判斷聯通塊即可

--------------------------------------------------------------------------------------------------------------------

自己查的資料:

歐拉路:如果從一個點出發,所有的邊經過一次,則稱爲歐拉道路,如果最後回到了起點,那麼成爲歐拉回路

無向圖存在歐拉回路的充要條件
一個無向圖存在歐拉回路,當且僅當該圖所有頂點度數都爲偶數,且該圖是連通圖。
有向圖存在歐拉回路的充要條件
一個有向圖存在歐拉回路,所有頂點的入度等於出度且該圖是連通圖。

參考自:https://baike.baidu.com/item/%E6%AC%A7%E6%8B%89%E5%9B%9E%E8%B7%AF/10036484?fr=aladdin

歐拉路專題資料:https://blog.csdn.net/devil2b/article/details/41413743



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章