poj2942 Knights of the Round Table

来源:互联网 发布:澳门娱乐网络博客 编辑:程序博客网 时间:2024/06/08 13:15

Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 12557 Accepted: 4183

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 51 41 52 53 44 50 0

Sample Output

2

今天第一次过来啃这个双联通分量,看了两天书,对着代码看着题研究一段了时间,今天动手敲了。

大意:

    有n个骑士参加圆桌会议,商讨大事。每次圆桌会议至少要有3个骑士参加,且相互憎恨的其实不能坐在圆桌旁的相邻位置。如果发生意见分歧,则需要进行举手表决,因此参加会议的骑士数目必须是奇数,以防止赞同和反对票一样多。知道哪些骑士相互憎恨后,你的任务是统计有多少个骑士不能参加任何一个会议。

思路:

如果两个骑士不相互憎恨,则给他们连一条边。

则问题可以转化成求不在任何一个简单奇圈上的结点个数。

因此步骤就是:

1.找到双联通分量。

2.对每个双联通分量进行01染色,成功表明这个圈是奇圈,那么所有的点进行标号即可,说明这些骑士可以凑一桌。

注意:割顶可能属于多个双联通分量,在染色时,要把这些点归于当前的联通分量。

关于双联通分量和01染色的学习,详细可以参考P316页。

#include <iostream>#include <cstdio>#include <vector>#include <stack>#include <cstring>using namespace std;const int MAXN=1000+5;int n,m;struct Edge{    int u,v;    Edge(int x,int y)    {        u=x;        v=y;    }};//查找双联通分量int pre[MAXN],bccno[MAXN],iscut[MAXN],dfs_clock,bcc_cnt;vector<int>G[MAXN],bcc[MAXN];stack<Edge>S;int dfs(int u,int fa){    int lowu=pre[u]=++dfs_clock;    int child=0,l=G[u].size();    for(int i=0; i<l; ++i)    {        int v=G[u][i];        Edge e=Edge(u,v);        if(!pre[v])        {            S.push(e);            child++;            int lowv=dfs(v,u);            lowu=min(lowu,lowv);            if(lowv>=pre[u])            {                iscut[u]=1;                bcc[++bcc_cnt].clear();                while(1)                {                    Edge x=S.top();                    S.pop();                    if(bccno[x.u]!=bcc_cnt)                    {                        bccno[x.u]=bcc_cnt;                        bcc[bcc_cnt].push_back(x.u);                    }                    if(bccno[x.v]!=bcc_cnt)                    {                        bccno[x.v]=bcc_cnt;                        bcc[bcc_cnt].push_back(x.v);                    }                    if(x.u==u&&x.v==v)break;                }            }        }        else if(pre[u]>pre[v]&&v!=fa)        {            S.push(e);            lowu=min(lowu,pre[v]);        }    }    if(fa<0&&child==1)iscut[u]=0;    return lowu;}void find_bcc(){    int i;    for(i=0;i<n;++i)    {        pre[i]=bccno[i]=iscut[i]=0;    }    bcc_cnt=dfs_clock=0;    for(i=0;i<n;++i)if(!pre[i])dfs(i,-1);}//判断奇圈,二分图,交叉染色int odd[MAXN],color[MAXN];bool bipartite(int u,int b){    int l=G[u].size();    for(int i=0;i<l;++i)    {        int v=G[u][i];        if(bccno[v]!=b)continue;        if(color[v]==color[u])return 0;        if(!color[v])        {            color[v]=3-color[u];            if(!bipartite(v,b))return 0;        }    }    return 1;}int tu[MAXN][MAXN];int main(){    int i,j;    while(~scanf("%d%d",&n,&m))    {        if(!n&&!m)break;        memset(tu,0,sizeof(tu));        for(i=0;i<n;++i)G[i].clear();        int u,v;        while(m--)        {            scanf("%d%d",&u,&v);            u--;            v--;            tu[u][v]=tu[v][u]=1;        }        for(u=0;u<n;++u)            for(v=u+1;v<n;++v)        if(!tu[u][v])        {            G[u].push_back(v);            G[v].push_back(u);        }        find_bcc();        memset(odd,0,sizeof(odd));        for(i=1;i<=bcc_cnt;++i)        {            int l=bcc[i].size();            memset(color,0,sizeof(color));            for(j=0;j<l;++j)bccno[bcc[i][j]]=i;            u=bcc[i][0];            color[u]=1;            if(!bipartite(u,i))            {                for(j=0;j<l;++j)odd[bcc[i][j]]=1;            }        }        int ans=n;        for(i=0;i<n;++i)if(odd[i])ans--;        printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击