poj2942 Knights of the Round Table

http://www.elijahqi.win/archives/3598
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.
Source

Central Europe 2005
tarjan求点双 判断每个点双是否存在奇环 存在奇环 则该点双内的点均可成立
最后用总数减即可
http://blog.sina.com.cn/s/blog_6c7729450100qwh1.html
证明:算法:双连通分量+二分图判定。

先不加证明地承认下列事实:

1.奇圈中的点必然属于同一个双连通分量中。

2.若某个双连通分量中存在一个奇圈,则该双连通分量中的所有点都存在于某一个奇圈中。

3.一个图存在奇圈当且仅当它不是二分图。

由以上三点,本题解法一目了然:首先求补图,然后DFS找出所有双连通分量,判断哪些双连通分量存在奇圈,标记存在奇圈的双连通分量的所有点,未被标记的点的数量即为所求。

附证明:

1.取u,v属于同一个奇圈,且u,v属于不同的双连通分量。则

(1)u,v都是割点,则(u,v)是桥(简单图中),与u,v属于同一个环矛盾;

(2)u,v中其中一个是割点,则u,v必属于同一个双连通分量;

(3)u,v均不是割点,则由双连通分量的定义,易知存在一点t属于该奇圈,且删去t后u,v不连通。这与u,v同属一个环矛盾。

2.考虑该双联通分量的点数|V|:

(1)|V|<=2,显然满足。

(2)|V|>2,易知此时分量内所有点的度>1,否则删去与它相连的唯一点,图变得不连通,与双连通分量的定义矛盾。则设Q是其中任一个奇圈,对于任意点v属于该连通分量,要么v属于Q,要么从v出发有2条边不交简单路径可到Q,因为如果只有1条简单路径或没有,就违反了双连通分量的定义。不妨设v不属于Q,设t1,t2是Q上可达v且路径不重复的2个点。假设t1-v-t2路径上共含有N个点:若N为奇数,因为t1,t2把Q分成2个部分,而Q含有奇数个点,所以t1,t2分成的2部分中必有一部分含有偶数个点,即t2-t1为偶数,故t1-v-t2-t1==奇数+偶数==奇数,即v存在于一个奇圈中;当N为偶数时类似。

3.证明很复杂,自己上网搜,主要是充分性比较麻烦。

#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int N=1100;
const int M=1e6+10;
struct node{
    int y,next;
}data[M<<1];
int h[N],dfn[N],low[N],n,m,num,col[N],mp[N][N],q[N],top,s;
bool mark[N],mk[N];vector<int> pd[N];
inline void insert1(int x,int y){
    data[++num].y=y;data[num].next=h[x];h[x]=num;
    data[++num].y=x;data[num].next=h[y];h[y]=num;
}
inline void tarjan(int x,int fa){
    dfn[x]=low[x]=++num;q[++top]=x;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if(y==fa) continue;
        if (!dfn[y]){
            tarjan(y,x);low[x]=min(low[x],low[y]);
            if (low[y]>=dfn[x]){
                ++s;pd[s].push_back(x);int v;
                do{
                    v=q[top--];pd[s].push_back(v);
                }while(v!=y);   
            }
        }else low[x]=min(low[x],dfn[y]);
    }
}
inline bool dfs(int x,int c){
    col[x]=c;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (!mk[y]) continue;
        if (col[y]==col[x]) return 0;
        if (col[y]!=col[x]&&col[y]!=-1) continue;
        if (!dfs(y,c^1)) return 0;
    }return 1;
}
int main(){
    freopen("poj2942.in","r",stdin);
    while(1){
        n=read();m=read();top=0;s=0;
        if (!n&&!m) return 0;memset(mp,0,sizeof(mp));
        //if (cnt==4) printf("%d %d\n",n,m);
        memset(h,0,sizeof(h));num=0;s=0;
        memset(mark,0,sizeof(mark));
        memset(dfn,0,sizeof(dfn));
        for (int i=1;i<=m;++i){
            int x=read(),y=read();
            //if (cnt==4) printf("%d %d\n",x,y);
            mp[x][y]=mp[y][x]=1;
        }
        for (int i=1;i<=n;++i)
            for (int j=1;j<i;++j)
                if (!mp[i][j]) insert1(i,j);num=0;
        for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i,0);
        for (int i=1;i<=s;++i){
            memset(mk,0,sizeof(mk));memset(col,-1,sizeof(col));
            for (int j=0;j<pd[i].size();++j) mk[pd[i][j]]=1;
            if (!dfs(pd[i][0],0)) 
            for (int j=0;j<pd[i].size();++j) mark[pd[i][j]]=1;
            pd[i].clear();
        }int ans=0;
        for (int i=1;i<=n;++i) ans+=mark[i];
        printf("%d\n",n-ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值