程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 2942 Knights of the Round Table (點雙連通分量求解)

poj 2942 Knights of the Round Table (點雙連通分量求解)

編輯:C++入門知識

Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 8763 Accepted: 2803

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

Hint

Huge input file, 'scanf' recommended to avoid TLE.

Source

Central Europe 2005


題意:(貼的別人的啦)

瑟王要在圓桌上召開騎士會議,為了不引發騎士之間的沖突,並且能夠讓會議的議題有令人滿意的結果,每次開會前都必須對出席會議的騎士有如下要求:
1、 相互憎恨的兩個騎士不能坐在直接相鄰的2個位置;
2、 出席會議的騎士數必須是奇數,這是為了讓投票表決議題時都能有結果。

如果出現有某些騎士無法出席所有會議(例如這個騎士憎恨所有的其他騎士),則亞瑟王為了世界和平會強制把他剔除出騎士團。
現在給定准備去開會的騎士數n,再給出m對憎恨對(表示某2個騎士之間使互相憎恨的),問亞瑟王至少要剔除多少個騎士才能順利召開會議?

注意:1、所給出的憎恨關系一定是雙向的,不存在單向憎恨關系。
2、由於是圓桌會議,則每個出席的騎士身邊必定剛好有2個騎士。即每個騎士的座位兩邊都必定各有一個騎士。
3、一個騎士無法開會,就是說至少有3個騎士才可能開會。


思路:

根據所給圖構出補圖,Tarjan求點雙連通分量(用棧保存邊),然後用交叉染色法判斷是否存在奇圈,有一個結論是雙連通分量如果存在一個奇圈,那麼這個分量中所有的點都能在某一個奇圈中,這樣就不必每個點都判斷了。


代碼:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 1000005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.000001
typedef long long ll;
using namespace std;

int n,m,ans,cnt,tot,flag,sum;
int dfn[maxn],low[maxn];
int vis[maxn],app[maxn],ok[maxn];
int edge[maxn][maxn];
int lt[maxn][maxn],xx[maxn];
struct Node
{
    int u,v;
} cur,sta[MAXN];

void presolve()
{
    int i,j,t;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            edge[i][j]^=1;
        }
    }
}
void dfs(int u,int fa)
{
//   printf("%d->",u);
    int i,j,t,v;
    if(fa) sta[++cnt].u=u,sta[cnt].v=fa;
    low[u]=dfn[u]=++tot;
    for(i=1; i<=n; i++)
    {
        if(!edge[u][i]||u==i) continue ;
        if(vis[i])
        {
            if(i!=fa) low[u]=min(low[u],dfn[i]);
        }
        else
        {
            vis[i]=1;
            dfs(i,u);
            low[u]=min(low[u],low[i]);
            if(dfn[u]<=low[i])
            {
                sum++;
 //               printf("sum:%d\n",sum);
                cur=sta[cnt];
                while(!(cur.u==u&&cur.v==i||cur.u==i&&cur.v==u))
                {
                    lt[sum][cur.u]=lt[sum][cur.v]=1;
//                    printf("%d-%d ",cur.u,cur.v);
                    cnt--;
                    cur=sta[cnt];
                }
                cnt--;
                lt[sum][u]=lt[sum][i]=1;
 //               printf("%d-%d\n",u,i);
            }
        }
    }
}
void color(int u,int c,int d)
{
    if(flag) return ;
    int i,j,t;
    for(i=1; i<=u; i++)
    {
        if(!edge[u][i]||i==u||!app[i]) continue ;
        if(vis[i]!=-1)
        {
            if(vis[i]==c)
            {
                flag=1;
                return ;
            }
        }
        else
        {
            vis[i]=c^1;
            color(i,c^1,d);
        }
    }
}
void solve()
{
    int i,j,t,u,v;
    memset(vis,0,sizeof(vis));
    memset(lt,0,sizeof(lt));
    sum=0;
    for(i=1; i<=n; i++)
    {
        if(!vis[i])
        {
            tot=cnt=0;
            vis[i]=1;
            dfs(i,0);
        }
    }
    memset(ok,0,sizeof(ok));
    for(i=1; i<=sum; i++)
    {
        t=0;
        memset(app,0,sizeof(app));
        for(j=1; j<=n; j++)
        {
            if(lt[i][j])
            {
                t++,app[j]=1;
                xx[t]=j;
            }
        }
//        printf("i:%d t:%d\n",i,t);
        for(j=1; j<=t; j++)
        {
            flag=0;
            memset(vis,-1,sizeof(vis));
            vis[xx[j]]=1;
            color(xx[j],1,i);
            if(flag) break ;
        }
        if(flag)
        {
            for(j=1; j<=t; j++)
            {
                ok[xx[j]]=1;
            }
        }
    }
    ans=0;
    for(i=1; i<=n; i++)
    {
        ans+=ok[i];
//        printf("i:%d ok[i]:%d\n",i,ok[i]);
    }
    ans=n-ans;
}
int main()
{
    int i,j,t,u,v,w;
    while(scanf("%d%d",&n,&m),n|m)
    {
        memset(edge,0,sizeof(edge));
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            edge[u][v]=edge[v][u]=1;
        }
        presolve();  // 求補圖
        solve();
        printf("%d\n",ans);
    }
    return 0;
}
/*
5 8
3 1
3 5
5 5
4 2
5 3
3 3
4 3
3 4
ans: 1
*/





  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved