[POJ]2942 圆桌骑士 点双连通分量 + 二分图染色

Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 13168 Accepted: 4393
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" andno” 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

题目大意

亚瑟王要召开圆桌会议,有些骑士之间互相憎恨不能邻座,参加会议的人数必须是奇数(以免同票),并且不能只有一个人.
现问多少骑士无论怎样安排也不能参加会议?
多组测试数据,以0 0为输入结束标志

题解

建个反图即可,得到可以邻座.根据题目中的条件,我们要找奇环,而一个点双连通分量有奇环则整个点双连通分量都可选,因为其他点也会在奇环里.可以假设整个点双连通分量为偶数(奇数没必要讨论了塞),再设与所包含奇环的公共边上的点数为x,分类讨论x为奇数,或偶数.你会发现,整个点双连通分量除了这个奇环剩下的环上的点数一定是奇数,包含了除奇环剩下所有的点.在自己证明的过程中不要忘记奇环上点数本身是奇数的事实.
tarjan求点双连通分量,交叉染色法求奇环,注意一个人虽是奇数但不能开会议.

Tips:
1.一个割点会出现在多个点双连通分量里,tarjan弹栈不要把它弹出了,但是要染色.或者说栈里保存边也可以.
2.交叉染色判奇环的时候注意不要01染色或12染色,还是因为一个割点出现在多个点双联通分量里,这个点很容易在已被01染色后在当前包含它的点双联通分量的交叉染色中产生矛盾.

#include<stdio.h>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
#define clear(a) memset(a,0,sizeof(a))
#define maxn 1010
bool mark[maxn],vis[maxn];
int point,all,indexx,cnt,num,n,m,x,y,ans;
int h[maxn],col[maxn],dfn[maxn],low[maxn],map[maxn][maxn],bcco[maxn];
stack<int> s;
struct edge{
    int v,nxt;
}e[1000010];
inline void add(int u,int v){e[++num].v=v;e[num].nxt=h[u];h[u]=num;}
inline int read(){
    register int x=0,f=1;
    register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f*=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return f*x;
}
bool color(int x, int c){
    col[x]=c;
    for(int i=h[x];i;i=e[i].nxt){
        int v=e[i].v;
        if(bcco[v]==cnt){
            if(col[v]==c) return false;
            else if(col[v]!=(c^1)&&color(v,c^1)==false) return false;
        }
    }
    return true;
}

void gotomark(int u){
    bcco[u]=-1,mark[u]=true;
    for(int i=h[u];i;i=e[i].nxt){
        int v=e[i].v;
        if(bcco[v]==cnt) gotomark(v);
    }
    return;
}
void tarjan(int u,int fa){
    dfn[u]=low[u]=++indexx;
    s.push(u),vis[u]=true;
    for(int i=h[u];i;i=e[i].nxt){
        int v=e[i].v;
        if(!vis[v]){
           tarjan(v,u);
           low[u]=min(low[u],low[v]);
           if(low[v]>=dfn[u]){
              cnt++;
              point=-1;
              while(point!=v){
                point=s.top();
                bcco[point]=cnt;
                s.pop();
              }
              bcco[u]=cnt;
              if(!color(u,u<<1)) gotomark(u);
           }
        }
        else if(dfn[v]<dfn[u]&&v!=fa) low[u]=min(low[u],dfn[v]);
    }
}
int main(){
    while(n=read(),m=read(),n||m){
        indexx=0,ans=0,num=0,cnt=0;
        clear(bcco),clear(low),clear(dfn),clear(col),clear(h),clear(map),clear(mark),clear(vis);
        for(register int i=1;i<=m;i++){
            x=read(),y=read();
            map[x][y]=map[y][x]=true;
        }
        for(register int i=1;i<=n;i++)
         for(register int j=i+1;j<=n;j++)
          if(!map[i][j])
           add(i,j),add(j,i);
        for(register int i=1;i<=n;i++) if(!vis[i]) tarjan(i,-1);
        for(register int i=1;i<=n;i++) 
         if(!mark[i]||!h[i])
          ans++;
        printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值