POJ 2942 Knights of the Round Table 补图+点双连通分量+染色法判二分图

http://poj.org/problem?id=2942

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.
题目大意:国王要在圆桌上召开骑士会议,但是有若干对骑士之间相互憎恨,出于各种奇怪的原因,每次开会都必须有如下要求:(1)相互憎恨的两个骑士不能坐在相邻的两个位置。(2)为了让投票表决议题时都能有结果,出席会议的骑士数必须是奇数。如果有某个骑士无法出席任何会议,则国王会为了世界和平把他踢出骑士团。现给出骑士总数 n n n和相互憎恨的关系,求至少要踢掉多少个骑士。

思路:我们可以建出原图的补图,若两个骑士没有憎恨关系,我们给他们连一条边。那么若干名骑士可以召开圆桌会议的条件就是:它们对应的节点组成一个长度为奇数的简单环。因此本题就是求有多少个点不被任何奇环包含,这些点就是要被踢掉的骑士。
引理一:
若两个骑士属于两个不同的点双连通分量,则它们不可能一起出席会议。
用反证法即可证明,若它们一起出席会议,则我们肯定可以得到一个更大的 v − D C C v-DCC vDCC(点双连通分量),而不是两个分散的 v − D C C v-DCC vDCC。这与 v − D C C v-DCC vDCC的极大性矛盾,故引理成立。
引理二:
若某个点双连通分量中存在奇环,则这个点双连通分量中的所有点都被至少一个奇环包含。
证明懒得抄了,略去。
引理三:
一张无向图是二分图,当且仅当图中不存在奇环。
证明略。
因此用 t a r j a n tarjan tarjan算法处理出所有的 v − D C C v-DCC vDCC,然后对每个 v − D C C v-DCC vDCC进行二分图判定(染色法)。若染色产生冲突,则该 v − D C C v-DCC vDCC中存在奇环,也就是说这个点双连通分量中的骑士至少可以参加一种会议,是满足题意的。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;

const int maxn=1005;
const int maxm=1e6+5;

struct edge
{
    int to,nxt;
}Edge[maxm<<1];

vector<int> dcc[maxn];//点双连通分量
bool cut[maxn],ans[maxn],tmp[maxn],s[maxn][maxn],tag=0;
int head[maxn],dfn[maxn],low[maxn],Stack[maxn],color[maxn];
int n,m,tot,num,root,top,cnt;

inline void addedge(int x,int y)
{
    Edge[++tot].to=y,Edge[tot].nxt=head[x],head[x]=tot;
}

void tarjan(int x)
{
    int y,flag=0;
    dfn[x]=low[x]=++num;
    Stack[++top]=x;
    if(x==root&&head[x]==0)//孤立点
    {
        dcc[++cnt].push_back(x);
        return ;
    }
    for(int i=head[x];i;i=Edge[i].nxt)
    {
        y=Edge[i].to;
        if(!dfn[y])//未访问过的节点
        {
            tarjan(y);
            low[x]=min(low[x],low[y]);
            if(low[y]>=dfn[x])
            {
                ++flag;
                if(x!=root||flag>=2)
                    cut[x]=1;
                cnt++;
                int z;
                do
                {
                    z=Stack[top--];
                    dcc[cnt].push_back(z);
                }while(z!=y);
                dcc[cnt].push_back(x);
            }
        }
        else
            low[x]=min(low[x],dfn[y]);
    }
}

void dfs(int x,int c)
{
    if(tag)
        return ;
    int y;
    color[x]=c;
    for(int i=head[x];i;i=Edge[i].nxt)
    {
        y=Edge[i].to;
        if(!tmp[y])
            continue;
        if(color[y]==0)
            dfs(y,3-c);
        else if(color[y]==c)
        {
            tag=1;
            return ;
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        int x,y;
        memset(head,0,sizeof(head));
        memset(dfn,0,sizeof(dfn));
        memset(cut,0,sizeof(cut));
        memset(ans,0,sizeof(ans));
        memset(s,0,sizeof(s));
        tot=1;
        num=cnt=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            s[x][y]=s[y][x]=1;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(!s[i][j])
                    addedge(i,j),addedge(j,i);
            }
        }
        for(int i=1;i<=n;i++)
            if(!dfn[i])
                root=i,top=0,tarjan(i);
        int re=0;
        for(int i=1;i<=cnt;i++)//判断每一个 v-DCC
        {
            memset(color,0,sizeof(color));
            memset(tmp,0,sizeof(tmp));
            for(int j=0;j<dcc[i].size();j++)
                tmp[dcc[i][j]]=1;
            tag=0;
            dfs(dcc[i][0],1);//染色判定
            if(tag)//存在奇环
            {
                for(int j=0;j<dcc[i].size();j++)//可参加会议
                {
                    if(!ans[dcc[i][j]])
                        ++re;
                    ans[dcc[i][j]]=1;
                }
            }
        }
        printf("%d\n",n-re);
        for(int i=1;i<=cnt;i++)
            dcc[i].clear();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值