Knights of the Round Table
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:
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 . 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 51 41 52 53 44 50 0 Sample Output 2 Hint Huge input file, 'scanf' recommended to avoid TLE. Source |
刘汝佳《算法竞赛入门经典--训练指南》P.316讲到这道题。
考虑题目给出的无向图的补图G(若两个骑士可以相邻,则在它们之间连一条无向边)。
题意即求图G中不在任何一个简单奇圈(奇圈是指长为奇数,亦即含奇数条边的圈)上的节点的数目。
对每个节点v判断并标记v是否在某个简单奇圈上即可。
简单圈上的所有节点必然属于同一个双连通分量。
现将图G分解成若干双连通分量,对每个双连通分量c,判断c中的每个节点是否在某个简单奇圈上。
如何判断一个图中是否含有奇圈?
我们知道一个无向图G是二分图的充要条件是G中不含有奇圈,因此可用二分染色来判断。
现在考虑那些不是二分图的双连通分量,虽然这些双连通分量一定含有奇圈,但是不是其中的每个节点都在某个奇圈上呢?