poj 2942 Knights of the Round Table

来源:互联网 发布:gre模考软件 知乎 编辑:程序博客网 时间:2024/05/22 09:09
Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 12277 Accepted: 4058

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

Hint

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

Source

Central Europe 2005

提示

题意:

亚瑟王要召开会议,有n个(1<=n<=1000)骑士将在圆桌开会,他们有m对(1<=m<=1000000)相互憎恨(没有单方面的),所以这m对人是不能坐到一起的(相邻),不然会掐起来。并且为了保证进行投票时不会因为票数一样而导致得不到最终结果。所以还要保证开会人数必须是奇数。为了世界核平请求出最少需要几个人被踢出会以才能让会议进行下去。

思路:

1.以憎恨关系建立图,但需要利用这个图再建立一个它的补图,补图表示的是能坐在一起的关系图。

2.求大于等于3个点的圈。用tarjan算法求双连通分量。

3.并且要保证点数为奇数。每求出一组就要判断是否为奇圈,二分图就是奇圈,那么判断二分图就用交叉染色法。

此题没有什么技巧,就是对图知识点的考查,知识点的综合性相当的高,弄清楚知识点再来解题吧。

这片博客没看懂那么给出一个传送门:http://blog.csdn.net/lyy289065406/article/details/6756821

接下来给出对于Tarjan算法相关知识的博客:

缩点:http://blog.sina.com.cn/s/blog_85e103a40100uy30.html

Tarjan的理解:http://blog.csdn.net/u011026968/article/details/10283071

双连通分量与强连通分量:http://www.cnblogs.com/szy-wlxy/p/4639316.html

示例程序

Source CodeProblem: 2942Code Length: 2988BMemory: 4484KTime: 1282MSLanguage: GCCResult: Accepted#include <stdio.h>#include <string.h>struct{    int u,v,pos,next,vis;}w[2000000];int h[1000],numw,id,top,map[1000][1000],color[1000],dfn[1000],low[1000],mark[1000],stack[2000000],oddpoint[1000];int min(int x,int y){    if(x<y)    {        return x;    }    else    {        return y;    }}void insert(int u,int v){    w[numw].u=u;    w[numw].v=v;    w[numw].pos=numw+1;    w[numw].next=h[u];    w[numw].vis=0;    h[u]=numw;    numw++;    w[numw].u=v;    w[numw].v=u;    w[numw].pos=numw-1;    w[numw].next=h[v];    w[numw].vis=0;    h[v]=numw;    numw++;}int find(int t){    int i,pos;    for(i=h[t];i!=-1;i=w[i].next)    {        pos=w[i].v;        if(mark[pos]==1)        {            if(color[pos]==-1)            {                color[pos]=(color[t]+1)%2;                return find(pos);            }            else if(color[pos]==color[t])            {                return 1;            }        }    }    return 0;}void judge(int t,int n){    int i;    memset(mark,0,sizeof(mark));    do    {        top--;        i=stack[top];        mark[w[i].u]=1;        mark[w[i].v]=1;    }while(w[i].u!=t);    memset(color,-1,sizeof(color));    color[t]=0;    if(find(t)==1)    {        for(i=0;n>i;i++)        {            if(mark[i]==1)//记录二分图上的点            {                oddpoint[i]=1;            }        }    }}void tarjan(int t,int n){    int i;    dfn[t]=id;    low[t]=id;    id++;    for(i=h[t];i!=-1;i=w[i].next)    {        if(w[i].vis==0)        {            stack[top]=i;            top++;            w[i].vis=1;//遍历过就不要再遍历了            w[w[i].pos].vis=1;            if(dfn[w[i].v]==-1)            {                tarjan(w[i].v,n);                low[t]=min(low[t],low[w[i].v]);                if(low[w[i].v]>=dfn[t])                {                    judge(t,n);//每有一个双连通分量就要判一次二分图                }            }            else            {                low[t]=min(low[t],dfn[w[i].v]);            }        }    }}int main(){    int n,m,i,i1,u,v,num;    scanf("%d %d",&n,&m);    while(n!=0||m!=0)    {        id=0;        numw=0;        top=0;        num=0;        memset(h,-1,sizeof(h));        memset(map,0,sizeof(map));        memset(dfn,-1,sizeof(dfn));        memset(oddpoint,0,sizeof(oddpoint));        for(i=1;m>=i;i++)        {            scanf("%d %d",&u,&v);            map[u-1][v-1]=1;//无向图            map[v-1][u-1]=1;        }        for(i=0;n>i;i++)        {            for(i1=i+1;n>i1;i1++)            {                if(map[i][i1]==0)//建立补图                {                    insert(i,i1);                }            }        }        for(i=0;n>i;i++)        {            if(dfn[i]==-1)//枚举点求双连通分量            {                tarjan(i,n);            }        }        for(i=0;n>i;i++)//不在双连通分量上的就必须开除        {            if(oddpoint[i]==0)            {                num++;            }        }        printf("%d\n",num);        scanf("%d %d",&n,&m);    }    return 0;}
0 0