POJ 2942 Knights of the Round Table - from lanshui_Yang

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:
  • 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.)
Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are many other possible reasons). If a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. These knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. To help Merlin, you have to write a program that will determine the number of knights that must be expelled. 

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 5
1 4
1 5
2 5
3 4
4 5
0 0

Sample Output

2
   題目大意:有n個騎士,他們之間可能有憎恨關係,例如:騎士a 憎恨 騎士b ,那麼騎士b 也一定 憎恨 騎士a。現在要給這n個騎士開會,會議需要滿足以下要求:
   1、參加會議的騎士數量必須是奇數。
   2、會議選用圓形桌子,即騎士們開會時圍成一個圈。
   3、要求開會時任意一個騎士與相鄰的兩個騎士之間沒有憎恨關係。
當然,會議可以開很多場,而且一個騎士也可以參加很多場會議(如果能參加的話),問:一場會議也不能參加的騎士數量是多少?
   解題思路:
   1、先說一下建圖方法:將兩個沒有憎恨關係的騎士之間連接一條邊(無向邊),代表開會時這兩個騎士可以相鄰。
   2、判斷一個騎士能否參加會議,就是判斷在這個無向圖中有沒有存在一個簡單的奇圈包含這個騎士(簡單奇圈是指由奇數個頂點組成的圈,並且這些頂點互不相同)。
   3、在同一個簡單奇圈上的點必然在同一個雙連通分量中,所以要找出圖中所有的雙連通分量,然後判斷這個雙連通分量中有沒有奇圈。二分圖中是沒有奇圈的,如果一個雙聯通分量不是二分圖,那麼一定存在奇圈(此處不再證明),所以只需判斷一個雙連通分量是不是二分圖即可,如果是二分圖,那麼這個連通分量中每個點都可以參加會議。
請看代碼:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#define mem(a , b) memset(a , b , sizeof(a))
using namespace std ;
inline void RD(int &a)
{
    a = 0 ;
    char t ;
    do
    {
        t = getchar() ;
    }
    while (t < '0' || t > '9') ;
    a = t - '0' ;
    while ((t = getchar()) >= '0' && t <= '9')
    {
        a = a * 10 + t - '0' ;
    }
}
inline void OT(int a)
{
    if(a >= 10)
    {
        OT(a / 10) ;
    }
    putchar(a % 10 + '0') ;
}
const int MAXN = 1005 ;
typedef struct edge
{
    int u ;
    int v ;
} E ;
vector<int>vert[MAXN] ;
vector<int>bscnt[MAXN] ; // 記錄每個雙連通分量裏的頂點
int blcnt[MAXN] ;  // 記錄每個頂點屬於哪個連通分量
bool ha[MAXN][MAXN] ; // 標記兩個騎士是否相互憎恨
bool vis[MAXN] ;
int n , m ;
int dfn[MAXN] ;
int low[MAXN] ;
int tmpdfn ;
int top ;
E stap[MAXN * MAXN] ;
bool odd[MAXN] ;
short color[MAXN] ;
int scnt ;
void clr()
{
    mem(ha , 0) ;
    mem(dfn , 0) ;
    mem(low , 0) ;
    mem(vis , 0) ;
    mem(blcnt , 0) ;
    mem(color , 0) ;
    mem(odd , 0) ;
    int i ;
    for(i = 0 ; i <= n ; i ++)
    {
        vert[i].clear() ;
        bscnt[i].clear() ;
    }
    top = -1 ;
    tmpdfn = 0 ;
    scnt = 0 ;
}
void tarjan(int u , int fa)
{
    int son = 0 ;
    vis[u] = true ;
    dfn[u] = low[u] = ++ tmpdfn ;
    int i ;
    for(i = 0 ; i < vert[u].size() ; i ++)
    {
        int v = vert[u][i] ;
        E e ;
        e.u = u ;
        e.v = v ;
        if(!vis[v])
        {
            stap[++ top] = e ;
            son ++ ;
            tarjan(v , u) ;
            low[u] = min(low[u] , low[v]) ;
            if(low[v] >= dfn[u])
            {
                scnt ++ ;
                int tu , tv ;
                while (1)
                {
                    E tmp = stap[top --] ;
                    tu = tmp.u ;
                    tv = tmp.v ;
                    if(blcnt[tu] != scnt)  // 注意此處的條件,由於關節點屬於
                                            //不同的連通分量,所以條件不能寫成(!blcnt[tu])
                    {
                        blcnt[tu] = scnt ;
                        bscnt[scnt].push_back(tu) ;
                    }
                    if(blcnt[tv] != scnt)
                    {
                        blcnt[tv] = scnt ;
                        bscnt[scnt].push_back(tv) ;
                    }
                    if(tu == u && tv == v)
                        break ;
                }
            }
        }
        else if(v != fa && dfn[v] < dfn[u])  // 注意此處的判斷條件,不要漏掉 dfn[v] < dfn[u]
        {
            stap[++ top] = e ;
            low[u] = min(low[u] , dfn[v]) ;
        }

    }
}
void init()
{
    clr() ;
    int i , j ;
    for(i = 0 ; i < m ; i ++)
    {
        int a , b ;
        RD(a) ;
        RD(b) ;
        ha[a][b] = ha[b][a] = true ;
    }
    for(i = 1 ; i <= n ; i ++)  // 建圖
    {
        for(j = 1 ; j <= n ; j ++)
        {
            if(!ha[i][j] && i != j)
                vert[i].push_back(j) ;
        }
    }
}
bool isbg(int u , int c)  //判斷是否爲二分圖
{
    int i ;
    for(i = 0 ; i < vert[u].size() ; i ++)
    {
        int v = vert[u][i] ;
        if(blcnt[v] != c)
            continue ;
        if(color[v] == color[u])
            return false ;
        if(!color[v])
        {
            color[v] = 3 - color[u] ;
            if(!isbg(v , c))
                return false ;
        }
    }
    return true ;
}
void solve()
{
    mem(vis , 0) ;
    int i ;
    for(i = 1 ; i <= n ; i ++)
    {
        if(!vis[i])
        {
            tarjan(i , -1) ;
        }
    }
    for(i = 1 ; i <= scnt ; i ++)
    {
        int j ;
        int tmp ;
        mem(color , 0) ;
        for(j = 0 ; j < bscnt[i].size() ; j ++)
        {
            tmp = bscnt[i][j] ;
            blcnt[tmp] = i ;
        }
        color[tmp] = 1 ;
        if(!isbg(tmp , i))
        {
            for(j = 0 ; j < bscnt[i].size() ; j ++)
            {
                tmp = bscnt[i][j] ;
                odd[tmp] = true ;
            }
        }
    }
    int ans = n ;
    for(i = 1 ; i <= n ; i ++)
    {
        if(odd[i])
            ans -- ;
    }
    OT(ans) ;
    puts("") ;
}
int main()
{
    while (scanf("%d%d" , &n , &m) != EOF)
    {
        if(n == 0  && m == 0)
            break ;
        init() ;
        solve() ;
    }
    return 0 ;
}


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