poj2942 点双连通图分量+二分图判定

Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 10291 Accepted: 3376

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 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. 

此题是大白书上的一道题目,刘汝佳证明了一个定理:一个点双连通图分量中如果存在一个奇圈,那么可以保证该分量中的所有点都至少在一个奇圈内,也就是该分量可以拆成若干个可以相交的奇圈,并且包含所有点(有些点可以在多个奇圈内),对应着每个奇圈围成一桌(一个人可以在多张桌上),也就是说该分量的所有人可以保留。相反,如果该分量中不存在奇圈,那么很显然无论如何都围不成一桌。这也就是证明了充分必要性。这刚好也是判定二分图的冲要条件。

还有一个问题是不同点双连通图分量之间的点就不能围成一桌吗?

这个问题可以看大白书:刘汝佳指出不同点双连通图分量之间最多一个交点(割点),那么横跨两个分量,显然必须经过割点两次,这显然是个交叉的圈,不能围成一桌。

看上面的前提是你已经掌握求解点双连通图分量的算法和二分图判定的染色算法,并且对一些基本图的概念有所理解。

求解点双连通图分量的算法:传送门点双连通分量

基本概念:传送门基本概念

注意:bcc[i]来记录第i个点双连通图分量包含的点,并用is[i]表示第i个点所在的点双连通图分量的编号。

记录这些是为了后边的二分图判定。

调试过程中发现自己对dfs理解还不够深,通过这段代码你可以彻底搞清dfs中4种边的本质!加油调试!

代码:

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

struct line{
    int fr,to,next;
    bool operator==(line &x)const{
        return (fr==x.fr&&to==x.to)||(fr==x.to&&to==x.fr);
    }
}p[Maxn*Maxn],st[Maxn*Maxn];
int adj[Maxn][Maxn],is[Maxn],color[Maxn];
int head[Maxn],dfn[Maxn],low[Maxn],vis[Maxn];
int tot,tmpdfn,top,cnt;
vector<int> bcc[Maxn];
void addedge(int a,int b){
    p[tot].fr=a;
    p[tot].to=b;
    p[tot].next=head[a];
    head[a]=tot++;
}
void dfs(int u,int fa){
    dfn[u]=low[u]=tmpdfn;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=p[i].next){
        int v=p[i].to;
        if(vis[v]&&dfn[v]<dfn[u]&&v!=fa){
            low[u]=min(low[u],dfn[v]);
            st[top++]=p[i];
        }
        else if(!vis[v]){
            st[top++]=p[i];
            tmpdfn++;
            dfs(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u]){
                cnt++;
                while(top--){
                    if(is[st[top].fr]!=cnt){
                        bcc[cnt].push_back(st[top].fr);
                        is[st[top].fr]=cnt;
                    }
                    if(is[st[top].to]!=cnt){
                        bcc[cnt].push_back(st[top].to);
                        is[st[top].to]=cnt;
                    }
                    if(st[top]==p[i]) break;
                }
            }
        }
    }
}
bool bipartite(int u,int cnt){
    for(int i=head[u];i!=-1;i=p[i].next){
        int v=p[i].to;
        if(is[v]!=cnt) continue;
        if(color[u]==color[v]) return false;
        if(!color[v]){
            color[v]=3-color[u];
            if(!bipartite(v,cnt)) return false;
        }
    }
    return true;
}
void solve(int n){
    cnt=0;
    for(int i=1;i<=n;i++) bcc[i].clear();
    memset(vis,0,sizeof vis);
    memset(is,0,sizeof is);
    for(int i=1;i<=n;i++){
        tmpdfn=1,top=0;
        if(!vis[i]) dfs(i,-1);
    }
    memset(vis,0,sizeof vis);
    for(int i=1;i<=cnt;i++){
        memset(color,0,sizeof color);
        for(int j=0;j<bcc[i].size();j++)
            is[bcc[i][j]]=i;
        color[bcc[i][0]]=1;
        if(!bipartite(bcc[i][0],i))
            for(int j=0;j<bcc[i].size();j++)
                vis[bcc[i][j]]=1;
    }
}
int main()
{
    int n,m,a,b;
    while(cin>>n>>m,n){
        memset(adj,0,sizeof adj);
        memset(head,-1,sizeof head);
        tot=0;
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            adj[a][b]=adj[b][a]=1;
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j&&!adj[i][j])
                    addedge(i,j);
        solve(n);
        int ans=n;
        for(int i=1;i<=n;i++)
            if(vis[i]) ans--;
        printf("%d\n",ans);
    }
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值