Knights of the Round Table
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 11805 | Accepted: 3870 |
Description
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country.
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
- The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
- An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ``yes" and ``no" have the same number of votes, and the argument goes on.)
Input
The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ).
The input is terminated by a block with n = m = 0 .
The input is terminated by a block with n = m = 0 .
Output
For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled.
Sample Input
5 5 1 4 1 5 2 5 3 4 4 5 0 0
Sample Output
2
五一回家前开始想,今天回学校算是整个搞懂了吧。
题意:要求存在几个点能构成奇数环,则这几个点可以留下,判断需要淘汰几个点
思路:题目给的是互相之间的讨厌,所以第一步先构造like的反图
targan判断双连通分量(环)
判断这个环是否是奇环(交叉染色法)
这里还有一个前提定理,只要双连通分量的一个点是奇数环,其他点也会存在奇数环将其包围,画一下就明白了
还是非常有考验的题目啦


/* ▓▓▓▓▓▓▓▓▓▓▓▓▓█▀Γ ,░░░░░░Θ█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓█╜ ,▄▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▄╬▒█▓▓▓▓▓▓██▀ΘΘ░░░░░░░░░░░░░É▀██▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒ ▓▓▓▓▓▓▓▓█░╓╣▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░Θ█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒ ▓▓▓▓▓▓▓░╓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓Θ░░░░░Θ█▒░░░ ░░░░░░░░'░░░░░░░░░░░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▓▓▓▒▒▒▒▒▒ ▓▓▓▓▓▓░▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒ΘΘΘΘ░░░░Θ░░░░╦░ ░░░δ░δδ░░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█Θ░░░░'' ░░░░░░░░░ ░░░░╫╣╬░█▓▓▓▓▓▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓Θ░░░ -░░░` ░░╢░░▒▒╬▓▓▓▓▒▓▒▒▒▓▓▓▒▓▓▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░ -⌐ "░╢╖⌐. '░╢░░░╠▒╣▓▓▓▓▓▒▓▒▒▒▒▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░ ,╥╢░ - -░ ░╢╦µ "░╢░░░╠▒▓▓▓▓▓▓▒▒▒▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░'.╣╣░░ -░ ░░ .░░ ░░▒▄ ░╢░░╢░╫▓▓▓▓▓▓▒▓▓▓▓▓▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░ ╔▒░░ ╤░ ░░ ░▓░ ░ ░ ⌐░╢▓µ ░░░░░░╢▓░█▓▓▒▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░' ╥▒░░ ╓╣░ ╫▒░ -╫▒╕ ░░ ░░ ░░╠▓╕ ░░░░░░░╢▓╣S▓▓▓▓▒▒▒▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓░░░ ║▓░░ ╣░░ ]▒░ ' ╠▓╠▌ ░▒░ ░╦ ░░ ░░║▓µ ░░░╠░░░╫▓╣░▓▓▒▓▒▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓Θ░░░ ║▓░░ - ╫Θ░░ ╫Θ░ ⌐ !▒Θ░▒ ░ ░░▓⌐ ░╢▌ ░░ ░░╢▓ ░░░░░╣░▒▌░╣▓▒▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▒░░░ ╓▓░░ ░⌐ ╠▌░░ ╔▒░░ ░ ║▓░░╫▌ ░░ ¡░╢▌ ░╫▓⌐ ░░░░░╫▌ ░░░░░░░░▓▓▓▓▓▒▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓░░░ ╓▓░░░ ░░ ╓▓░░░ ╫Θ░ ░ ╫▒░░░▓µ ░ ░░▒╕ '░╫▌µ ░░░░░▓µ ░░░░░░░╫▒Θ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓Θ░░⌐╓▓▓░░░ ░░⌐ .╣▒░░ ]▓░░░░░ ]▓░░░░║▓ ░╦⌐░░╠▓µ ░░▓▓╦ ░░░░╫▌ ░░░░░░║▓░░▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓░░░╓▓▒▒░░⌐░╢░ ║▓░░░ ║▓░░░░⌐ ╠▒░░ ░╫▌ ╠░░░░▓▓ "░░▓▓░░ ░░░╠▒µ ░░░░░░▓░░╚▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓░░░╫▒╫Θ░░░░░░ ╔▓▒░░░ ╫▌░░░░ ▒░░░ ░▒╕ \░░░░╠▓▌ ░╢╠▓▓░░⌐ ░░░╫░ ░░░░░░▒▌░░▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▌░░╫▓░╣░░░░░░⌐ ╫▒▒░░░ ]▓▌░░░░ ]▒░░ ░▒╕ ░░░░░╫▓▌ ░░╢▒▒▒░░⌐ ░░╫▌ ░░░░░░║▌░▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▌░╢▓▓░╣░░░░░░ ╠▒╢▓░░░ ╫▒▒░░░░ ║▒░░ ░▒╕ ░░░░░▓▒▌ \░░╢▓δ▓░╣░ '░╢▓░ ░░░░░░╠▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▌╠▓▓▓░╣╣░░░░░⌐ ▒░╫▒░░░ ]▒╣▒░░░░ ╫░░░ ╙▒Q ░░░░╢▓▒▓ "░░╢▓░▒╣░░░ ░╢▓░ ░░░░░░╠▓░╫▓▓▓▓▒▒ ▓▓▓▓▓▓▓▓▓▌╫▓▓▓░╫▒░░░░░ ║▒░║▓░░░ ║▒░▒░░░░]▒░░ `▒▌ ░░░░╫▒░▓╕ ░░░▒▌░Θ▓░░░░░▒C ░░░╢░░▐▓░░╫▓▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░╫▓░░░░░.╫░░▐▓░░░ ▒░░╫╣░░░]▒░░ ╙▓╕░╢░░▒░░Θ▒µ⌠░╣╫▒░░╫▓░░░▒▌ ░░░░░░▐▓░╢▓▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░▓░░░░░▐▒░░ ╫▒░░░▒░░║▓░░░▐▒░░ '▒╦░░╢╠▓░░╙▀╦░░░▒▓░░░▒╣░▒▌░ ░░░░╢░▐▓▓▓▒▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░║▓░░░░╫▌░░ ╚▓░░░▒░░░╢▌░░╠▌░░ ╙▒░░░╫▒░░⌐ ▀▒░░╢▓░░░Θ▓▒▌░⌐░╢░░░░╠▓▒▓▒▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░░╫▓╢░░▒░êè╗╗╣▓░░▒░░ '╫░░╠▒░░ ╙▒╣░▒░░░ ,╠▒▓╣▓▒▒Θ░▒░░⌐░░░╣░░╠▓░╫▓▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░░░╫▓░░▒░░░░░░║▒Θ▓█▓▓▓▄▓▓╢▒░░ ,▄▓▓▓▓▓▓▓╢░░▄░▒▓▒░░▒░░░░░░░░░╠▓░║▓╣▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░ ░╣╚▓░▒░▄▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▄ ▄╬▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██▓▓▓▒░░░░░░░░░╠▓╣▓▒▒▒▒╣▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░ ░░░║▓▓▓▓█δ░ ╫▓▓▓▓▓▓▓▓▓▓▓▓ΘÑδòµ ⌐ôΘ░░║▓▀▓▓▓▓▓▓▓▓▓▓▓▌░░░▓▓▒░░░░░░░░░║▓▒▒▒▒▒╣▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░ ░░░▐▓░░▒░" ╙▀▀█▓▓▓▓▓▓▓░"δ░ '░╜ `^`▓▓▓▓▓▓▓▓▌'░╢Ö░▒░░░░░░░╣░║▓▒▒▒▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░ ░░░░╫▓░░░ ╫▓▓▓▓▓▓▓▓▓▓░ '▓▓▒▒▓▓▓▒▒▒▓░╓░░░╠▌░░░░░╣░░░╫▓▓▓▓╣▒▒▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░ ░░░░╢▓░░⌐ "è▄▓▓╣╣╢╣╬╣▓▓░ :██▒▒▓▓▓▓▓▓█ò░░░░╫Θ░╢░░╣░╢░░▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▌░░ ░░'░░▓▓░ ``▀▀▀▀Γ`` ⌐░░░░░░░╫░░░░░░░░░░▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░⌐ ░░▒▓▌░ ░░░░░░░╣▓⌐░╢░░░░╣░║▓▓▒▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░ ░ ░░╫▒▒▌ ░░░░░░░╫▒▒ ░░░░░╢╣░╫▓▒▒▒▒▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓░╣░ ░░╢▓░▒▄ '░░░░░░╠▓╣▌ ░╢░░░░╢░▓▓▒▒▒▒▒▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓░▒░⌐ ░░╫▓▓╣▓╕ ' ░░⌐ ░░░░░░║▓▓▓▌ ░╢░░░╢░║▓▓▒▒▒▒▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓░╫░░ ░░╢║▓▓▓▓▓▄ ░░░░⌐ ░░░░░░║▓▓▓▓░ ░░░░░╣░▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒╫▌ ░░╠▓▓▓▓▓▓▓▄ ' ░░░░░╓▓▓▓▓▓▌ ░╢░╣░╣╢▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▒▓▓╫▓░ ░╢╢▓▓▓▓▓▓▓▓▓▄ ╙╜╨╨╨╨╨ÉSê░ ░░░░╓╬▓▓▓▓▓▓▌' ░╢╣╣╢░╫▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓╣▓▓╢▓░ ░░╢▓▓▓▓▓▓▓▓▓▓▓▄ ⌐░░░╥▓▓▓▓▓▓▓▓▓░░ ░╢░╣╢░▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░▓░ ░╢╠▓▓▓▓▓▓▓▓▓▓▓▓▓▓▄ ⌐░░░╣▓▓▓▓▓▓▓▓▓▓▓░░░░╢░╣░╫▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌▒░░⌐░░╢▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓╦µ ░╓╗▒Θ╫▓▓▓▓▓▓▓▓▓▓▓ ╫░░╢░░░▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓╢▒░ ░╢╢▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░╙▒╦µ ╔╣▒Θ░░░╫▓▓▓▓▓▓▓▓▓▓▌░▒⌐░╢░░╫▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓╣▓░░░╢╢▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░ ╙δ╣╦, ,╓╗▒▒░░░░░░░╫▓▓▓▓▓▓▓▓▓▓▌▐▒⌐░╢╢░▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░╢╢▓▓▓▓▓▓▓▓▓▓▓█▓▓▓▓░ `ÉΘ▒▒▒▒ΘΘ░░░░░░░░░░░╢▓▓▓▓▓▓▓▓▓▓▌╠▒░╠╢░╫▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░╢╢▓▓▓▓▓▓▓▓▓▓░░░░╙▀▀▀▀▀▀S▒▒▒▒▒▒▒╣╣╣▒▒╬▒▒▒▒▒▒▒▓▓▓▒▒╣╫▓▓▓▓▓▌║▌⌐░░░▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓╣░╢╢▓▓▓▓▓▓▓▓▓▒╫▓▓▓▓▄▄▄▄,,, ░░░░╢╢╣╣╣╣╣╣▓▓▓▓▓▓▓▓▓▓▓▓▌╫▌ ░░╫▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░╫▓▓▓▓▓▓▓▓╬▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓µ╔▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌▒▌ ░╠▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 888 d8P d8b d8888 888 888 888 d8P Y8P d88888 888 888 888 d8P d88P888 888 888 888d88K 888 88888b. .d88b. d88P 888 888d888 888888 88888b. 888 888 888d888 8888888b 888 888 "88b d88P"88b d88P 888 888P" 888 888 "88b 888 888 888P" 888 Y88b 888 888 888 888 888 d88P 888 888 888 888 888 888 888 888 888 Y88b 888 888 888 Y88b 888 d8888888888 888 Y88b. 888 888 Y88b 888 888 888 Y88b 888 888 888 "Y88888 d88P 888 888 "Y888 888 888 "Y88888 888 888 Y8b d88P "Y88P" .d8888b. 888 d88P Y88b 888 Y88b. 888 "Y888b. 8888b. 88888b. .d88b. 888d888 "Y88b. "88b 888 "88b d8P Y8b 888P" "888 .d888888 888 888 88888888 888 Y88b d88P 888 888 888 d88P Y8b. 888 "Y8888P" "Y888888 88888P" "Y8888 888 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <string> #include <vector> const int inf = 0x3f3f3f; const int MAXN = 1e3+10; const int MMAXN = 1e6+10; struct edge{ int next; int st; int to; int vis; }; using namespace std; int n,m; stack<int>s; int tG[MAXN][MAXN]; edge e[MMAXN]; int top; int first[MAXN]; int dfn[MAXN]; int low[MAXN]; int mark[MAXN]; int col[MAXN]; int odd[MAXN]; void init(){ top = 0; memset(tG,0,sizeof(tG)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(odd,0,sizeof(odd)); memset(first,-1,sizeof(first)); while(!s.empty()){ s.pop(); } } void addege(int u,int v){ e[top].st = u; e[top].to = v; e[top].next = first[u]; e[top].vis = 0; first[u] = top++; } //交叉染色法判断是否为奇环 int find(int u){ int v; for(int i=first[u];i!=-1;i=e[i].next){ v = e[i].to; if(mark[v]){ if(col[v]==-1){ col[v] = !col[u]; return find(v); } else if(col[v]==col[u]) return 1; } } return 0; } void color(int u){ int tmp; memset(mark,0,sizeof(mark)); tmp = s.top(); while(e[tmp].st!=u){ mark[e[tmp].to] = 1; mark[e[tmp].st] = 1; s.pop(); tmp = s.top(); } mark[e[tmp].st] = 1; mark[e[tmp].to] = 1; s.pop(); memset(col,-1,sizeof(col)); col[u] = 1; if(find(u)){ for(int i=1;i<=n;i++){ if(mark[i]){ odd[i] = 1; } } } } void dfs(int u,int step){ dfn[u] = low[u] = step; int v; for(int i=first[u];i!=-1;i=e[i].next){ if(e[i].vis)continue; e[i].vis = e[i^1].vis = 1; s.push(i); v = e[i].to; if(!dfn[v]){ dfs(v,step+1); low[u] = min(low[u],low[v]); if(low[v]>=dfn[u])color(u); //每次不是双连通块了就要及时更新 } else low[u] = min(low[u],dfn[v]); } } int main() { int a,b; while(scanf("%d%d",&n,&m),n){ int ans=0; init(); for(int i=0;i<m;i++){ scanf("%d%d",&a,&b); tG[a][b] = 1; } //构造反图 for(int i=1;i<n;i++){ for(int j=i+1;j<=n;j++){ if(!tG[i][j]){ addege(i,j); addege(j,i); } } } //寻找双连通块(环) for(int i=1;i<=n;i++){ if(!dfn[i]){ dfs(i,1); } } for(int i=1;i<=n;i++){ if(!odd[i]){ ans++; } } cout<<ans<<endl; } return 0; }