uva1364 - Knights of the Round Table 点-双联通分量

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$ \le$n$ \le$1000 and 1$ \le$m$ \le$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

   割顶:对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或割顶。对于连通图,割顶就是删除之后使图不再连通的点。

  桥:如果删除(u,v)这条边让图G非连通,(u,v)这条边称为桥。

  对于一个无向连通图,如果任意两点至少存在两条点不重复路径,这个图是点-双连通的。等价于任何两条边都在同一个简单环中,即内部无割顶。

  如果任意两点至少存在两条边不重复路径,这个图是边-双连通的。等价于每条边都至少在一个简单环中,即所有边都不是桥。


  这个题是有N个人,坐圆桌开会,每次至少有3个人参加并且是奇数,且相互憎恨的人不能坐相邻位置。问有多少个人不能参加任何一场会议。

  把可以相邻的人建边,题目转化为求不在任何一个简单奇圈的节点个数。

  简单圈所有节点属于同一个点-双连通分量,二分图是没有奇圈的,所以先找出不是二分图的点-双连通分量。如果节点v属于某个不是二分图的双连通分量,v一定属于一个奇圈,因为假设奇圈为C,根据连通性,从v可以到C中某个点u1,根据双连通性,v能不经过u1从另一条路径到C中另一个点u2,在C中,u1到u2的路径长一奇一偶,所以v,u1,u2一定能在一个奇圈中。

  所以最后方法是先求出双连通分量,判断是不是二分图,不是的话把里面的点都标记,最后用N减去被标记的点。

  求双连通分量的方法是如果点u存在某个子节点的low[v](v及v的后代能连回的最早的祖先的pre(时间戳))大于等于pre[u],说明u是割顶,因为如果把u去掉,v的连通子图不能和u之前的连通子图连通。栈中存的边,这时退栈到(u,v)这条边为止,退栈的这些边对应的点都属于同一个点-双连通分量。


#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 1010
#define MAXM 2000010
#define MAXNODE 105
#define MOD 100000
#define SIGMA_SIZE 4
typedef long long LL;
using namespace std;
int N,M,vis[MAXN][MAXN],odd[MAXN],color[MAXN],pre[MAXN],iscut[MAXN],bccno[MAXN],dfs_clock,bcc_cnt;
vector<int> V[MAXN],bcc[MAXN];
struct Edge{
    int u,v;
};
stack<Edge> S;
int dfs(int u,int fa){
    int lowu=pre[u]=++dfs_clock;
    int child=0,L=V[u].size();
    for(int i=0;i<L;i++){
        int v=V[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_cnt++;
                bcc[bcc_cnt].clear();
                for(;;){
                    Edge x=S.top();
                    S.pop();
                    if(bccno[x.u]!=bcc_cnt){
                        bcc[bcc_cnt].push_back(x.u);
                        bccno[x.u]=bcc_cnt;
                    }
                    if(bccno[x.v]!=bcc_cnt){
                        bcc[bcc_cnt].push_back(x.v);
                        bccno[x.v]=bcc_cnt;
                    }
                    if(x.u==u&&x.v==v) break;
                }
            }
        }
        else if(pre[v]<pre[u]&&v!=fa){
            S.push(e);
            lowu=min(lowu,pre[v]);
        }
    }
    if(fa<0&&child==1) iscut[u]=0;
    return lowu;
}
void find_bcc(){
    memset(pre,0,sizeof(pre));
    memset(iscut,0,sizeof(iscut));
    memset(bccno,0,sizeof(bccno));
    dfs_clock=bcc_cnt=0;
    for(int i=1;i<=N;i++) if(!pre[i]) dfs(i,-1);
}
int bipartite(int u,int b){
    int L=V[u].size();
    for(int i=0;i<L;i++){
        int v=V[u][i];
        if(bccno[v]!=b) continue;
        if(color[u]==color[v]) return 0;
        if(!color[v]){
            color[v]=3-color[u];
            if(!bipartite(v,b)) return 0;
        }
    }
    return 1;
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&N,&M)!=EOF&&(N||M)){
        for(int i=1;i<=N;i++) V[i].clear();
        memset(vis,0,sizeof(vis));
        int u,v;
        while(M--){
            scanf("%d%d",&u,&v);
            vis[u][v]=vis[v][u]=1;
        }
        for(int u=1;u<=N;u++)
            for(int v=u+1;v<=N;v++) if(!vis[u][v]){
                V[u].push_back(v);
                V[v].push_back(u);
            }
        find_bcc();
        memset(odd,0,sizeof(odd));
        for(int i=1;i<=bcc_cnt;i++){
            memset(color,0,sizeof(color));
            int L=bcc[i].size();
            for(int j=0;j<L;j++) bccno[bcc[i][j]]=i;
            int u=bcc[i][0];
            color[u]=1;
            if(!bipartite(u,i)) for(int j=0;j<L;j++) odd[bcc[i][j]]=1;
        }
        int ans=N;
        for(int i=1;i<=N;i++) if(odd[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、付费专栏及课程。

余额充值