(点双联通分量模板)POJ 2942 Knights of the Round Table 圆桌骑士

Knights of the Round Table

Time Limit: 7000MS Memory Limit: 65536K
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.

题目大意:
一个国家有N(1<=n<=1000)个圆桌骑士,该国有一种会议叫圆桌会议,即由奇数个(>1)骑士围成圆桌开会,已知M对禁止关系(a,b)表示编号为ab的两名圆桌骑士不能相邻坐,询问该国有多少名骑士无论如何安排座位都可能参加任何一场会议。

思路:
题目给出是禁止关系,要求不能参加任何会议的骑士数量,我们建边一般是构造一些关联关系,而这里我们无法直接处理不相邻关系(不相邻的也可以坐一桌),所以我们考虑算可以开会的人数,可以根据可以相邻的骑士建立一个无向图,两名骑士之间存在边表示这两名骑士可以相邻坐,那么一名骑士能参与圆桌会议的条件就是此图中存在至少一个过这名骑士的一个奇环(环就是首尾相接的可以相邻关系)。
此处有一个结论,若存在一个奇环,则所有与此奇环存在公共边的环上的点都可选(假设奇环分为弧AB与弧CD(A.C重合,B.D重合),那么肯定其中一个有奇数条边,一个有偶数条边,假设弧AB有奇数条边,弧CD有偶数条边,另一个环与原环有公共边,那么肯定被原环截成两个弧,弧EF,弧GH。与原环上一奇一偶两条弧一定会构成奇环)。
所以如果存在一个奇环,那么此与此奇环相关的点双联通分量中所有的点都是可选。
于是就可以用tarjan求出原图的所有点双联通分量,在每个点双联通分量中判断是否存在奇环(二染色判定,出现矛盾就说明有奇环,本题的染色比较巧妙,下面代码中会详细介绍),若有奇环则标记此分量中所有点为可选,答案就是所有不可选的数量。

多组数据记得memset。tarjan的算法较多,不要混淆了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;

const int N=1010;
const int M=1000010;

int n, m, k, l;
int idx,idc,cnt;
int head[N],dfn[N],low[N],place[N],color[N],vis[N];
int a[N][N],no[N];
stack <int> state;

struct Edge{
    int to, next;
}ed[M];

void adde(int u, int v){
    ed[++idc].to = v;
    ed[idc].next = head[u];
    head[u] = idc;
}

bool col(int x, int c){
    color[x] = c;
    for(int i=head[x]; i; i=ed[i].next){
        int v = ed[i].to;
        if(place[v] == cnt){//在同一点连通分量中 
            if(color[v] == c) return false;//出现矛盾
            else if(color[v] != (c^1) && col(v, c^1) == false) return false;
        }
    }
    return true;
}

void mark(int x){
    no[x]=1; place[x]=-1;
    for(int i=head[x]; i; i=ed[i].next){
        int v = ed[i].to;
        if(place[v] == cnt) mark( v );//标记可以参加的人 
    }
    return;
}

void tarjan(int u, int fa){
    dfn[u] = low[u] = ++idx;
    vis[u] = 1;// child++;
    state.push(u);
    for(int i=head[u]; i; i=ed[i].next){
        int v = ed[i].to;
        if( !vis[v] ){
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if(low[v] >= dfn[u]){
                //iscut[u] = 1;u是割点 
                cnt++;
                int t = -1;
                while(t != v){//v
                    t = state.top();
                    place[t] = cnt;
                    state.pop();
                }
                //t = state.top();
                //place[t] = cnt;
                place[u] = cnt;//求点双联通分量时要包含割点 
                if( !col(u, u<<1) ) mark(u);//从割点染色(很秒的染色方法) 
                //这里最好不用01染色(不用固定的两种颜色染色);
                //因为一个奇环上或许有多个割点,这些割点可能是之前在其他奇环中已经染过色的;
                //那么我们并不能确定当前这个割点到底染0还是1(如果有两个割点相邻都染成0,就判断错误了) 
                //当然也可以每次都尝试染两种颜色(应该吧)不过太麻烦了
                //而这种染色方式带有点标号的属性,每次都是不同的两种颜色
                //(既可以识别当前奇环上的染色,又可以排除掉之前染色的影响),就避免了这个问题 
            }
        }
        else if( dfn[v] < dfn[u] && v != fa ){//避免统计到桥 
            low[u] = min(low[u], dfn[v]);
        }
    }
    //if( fa<0 && child==1 ) iscut[u] = 0; 
}

int main(){
    while( scanf("%d%d", &n, &m) ){
        if( !n && !m ) return 0;;
        idc = 1; idx = cnt = 0;
        memset(place, 0, sizeof(place));
        memset(vis, 0, sizeof(vis));
        memset(head, 0, sizeof(head));
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(color, 0, sizeof(color));
        memset(no, 0, sizeof(no));
        memset(a, 0, sizeof(a));
        for(int i=1; i<=m; i++){
            int u, v; 
            scanf("%d%d", &u, &v);
            a[u][v] = 1; a[v][u] = 1;
        }
        for(int i=1; i<=n; i++)
            for(int j=i+1; j<=n; j++)
            if( a[i][j] != 1 ){
                adde(i, j); adde(j, i);
            }
        for(int i=1; i<=n; i++)
            if( !vis[i] ) tarjan( i, -1 );
        int ans = 0;
        for(int i=1; i<=n; i++)
            if(!no[i] || !head[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、付费专栏及课程。

余额充值