POJ 2942 Knights of the Round Table 点双联通分量

Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 13183 Accepted: 4401
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
Hint

Huge input file, ‘scanf’ recommended to avoid TLE.

题意:有一堆骑士,其中有一些骑士相互憎恨。现在他们要举行一个圆桌会议,参加会议的骑士数目至少是3个且必须是奇数,相互憎恨的两个骑士不能挨着坐。问有多少个骑士不能参加所有的会议。

分析:
这是蓝书上的一道题,而我是听学长讲了后才发现这道题的…可怕。
蓝书上讲的很详细,所以只简单分析。
对每个骑士,与和他互不相恨的骑士连一条无向边,题目就变成了求不在任意一个简单奇圈(包含奇数个节点的回路)上的骑士数目。
因为二分图没有奇圈,所以我们对每个连通分量判断是否为二分图(利用染色的方法)如果不是就把该连通分量上的每一个点标记一次。最后统计没有标记过的点数就行了。

注意点双联通分量(任意两点至少存在两条“点不重复”的路径)的求法:
与强连通分量类似,但注意压入栈的是边而不是点。
如果能够构成连通分量,则low[v]>=dfn[u] ;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;

const int N = 1000 + 10;
const int M = 1000000 + 1000;

int n,m;
int hate[N][N];
struct node{
    int pre,v,u;
}edge[2*M];

int num=1;
int head[N];

void addedge(int from,int to){
    num++;
    edge[num].pre=head[from];
    edge[num].v=to;
    edge[num].u=from;
    head[from]=num;
}

int indx=0;
int low[N],dfn[N];
int top=0,stack[M];
bool vis[M*2],ifw[N];
int cnt=0,place[N];
int color[N];

bool check(int u){
    for(int i=head[u];i;i=edge[i].pre){
        int v=edge[i].v;
        if(place[v]==cnt){
           if(color[u]!=0&&color[u]==color[v]) return true;
           if(!color[v]){
              color[v]=3-color[u];
              return check(v);
           }
        }
    }
    return false;
}//利用二分图染色来检查 

inline int Min(int a,int b){
    return a<b?a:b;
}

void Tarjan(int u){
    low[u]=dfn[u]=++indx;
    for(int i=head[u];i;i=edge[i].pre){
        if(vis[i]) continue;
        vis[i]=vis[i^1]=true;
        top++;stack[top]=i;//把边压入栈 
        int v=edge[i].v;
        if(!dfn[v]){
            Tarjan(v);
            low[u]=Min(low[u],low[v]);
            if(low[v]>=dfn[u]){
                cnt++;
                int j=0;
                while(edge[j].u!=u){
                    j=stack[top];
                    place[edge[j].u]=cnt;
                    place[edge[j].v]=cnt;
                    top--;
                }
                ms(color,0);
                color[u]=1;
                if(check(u)){
                    for(int j=1;j<=n;j++)
                       if(place[j]==cnt) ifw[j]=true;
                }
            }
        }
        else if(dfn[v]) low[u]=Min(low[u],dfn[v]);
    }
}

void update(){
    num=1,indx=0,cnt=0,top=0;
    ms(hate,0);ms(dfn,0);ms(low,0);ms(vis,0);ms(ifw,0);
    ms(place,0);ms(color,0);ms(head,0);ms(stack,0);
}

int main(){
    while(1){
       scanf("%d%d",&n,&m);
       if(n+m==0) break;
       update();
       for(register int i=1;i<=m;i++){
          int u,v;
          scanf("%d%d",&u,&v);
          hate[u][v]=hate[v][u]=1;
       }
       for(register int i=1;i<n;i++){
          for(register int j=i+1;j<=n;j++){
             if(!hate[i][j]){
                 addedge(i,j);addedge(j,i);
             }
          }
       }
       for(register int i=1;i<=n;i++) if(!dfn[i]) Tarjan(i);
       int ans=0;
       for(register int i=1;i<=n;i++){
           if(!ifw[i]) ans++;
       }
       printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值