【poj 2942 】 Knights of the Round Table 【tarjan求bcc+黑白染色判二分图】

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.

日常补充 知识点 TAT
1 对于一个双连通分量
1)如果存在奇环,那么这个双连通分量中的每一个点都一定会存在于至少一个奇环中。【因为任何一个点都有两条以上到这个奇环的路径,两条路径在连接奇环上的两个点,可以把奇环变为偶链和奇链 这样根据该点到这两个点间点个数的奇偶性进行选择 就保证一定可以构成奇环。】
2)同样,如果不存在奇环,则所有双连通分量中的点都不存在于任何一个奇环中。

2 如果一个双连通分量不是一个二分图,那它一定含有奇圈。反过来也成立,这是一个充要条件。

3 一个连通图中,所有的bcc分量 都不会是单独的一个点,至少有两个,只有两个点才可能为bcc分量 。

思路:在没有仇恨关系的骑士之间 建双向边【建立补图】,构图然后求出所有BCC并把各个BCC里面的点分别存储起来。最后依次判断每个BCC,若不是二分图,则标记该BCC里面所有骑士,说明他们在奇圈里面。最后判断有多少个骑士不在奇圈即可。

代码

#include<cstdio>
#include<cstring>
#include<queue>
#include<stack> 
using namespace std ;
#define LL long long 
const int MAXN =1000+10;
const int MAXM =1e6+100;
const int inf =0x3f3f3f3f;
/*---------------------*/
struct Edge {
    int from,to,nexts;
}edge[MAXM];
int n,m;
int head[MAXN],top;
bool mp[MAXN][MAXN]; // 矛盾关系
void init(){
    memset(head,-1,sizeof(head));
    top=0;
}
void addedge(int a,int b){
    Edge e={a,b,head[a]};
    edge[top]=e;head[a]=top++;
}
void getmap(){
    memset(mp,0,sizeof(mp));
    while(m--){
        int a,b;
        scanf("%d%d",&a,&b);
        mp[a][b]=mp[b][a]=1;
    }
    for(int i=1;i<=n;i++){    // 建立补图 
        for(int j=i+1;j<=n;j++){
            if(!mp[i][j]) {
                addedge(i,j);
                addedge(j,i);
            }
        }
    }
}
int dfn[MAXN],low[MAXN];
int bccno[MAXN],bcc_cnt;
vector<int>bcc[MAXN]; // 存bcc中的点
stack<Edge>S; 
int dfs_clock;
int iscut[MAXN];

void tarjan(int now,int pre){
    low[now]=dfn[now]=++dfs_clock;
    int son=0;
    for(int i=head[now];i!=-1;i=edge[i].nexts){
        Edge e=edge[i];
        if(!dfn[e.to]){
            S.push(e);
            son++;
            tarjan(e.to,now);
            low[now]=min(low[now],low[e.to]);
            if(low[e.to]>=dfn[now]){
                iscut[now]=1;
                bcc_cnt++;
                bcc[bcc_cnt].clear();
                for(;;){
                    Edge ee=S.top();S.pop();
                    if(bccno[ee.from]!=bcc_cnt){
                        bcc[bcc_cnt].push_back(ee.from);
                        bccno[ee.from]=bcc_cnt; 
                    }
                    if(bccno[ee.to]!=bcc_cnt){
                        bcc[bcc_cnt].push_back(ee.to);
                        bccno[ee.to]=bcc_cnt;
                    }
                    if(ee.from==now&&ee.to==e.to) break; 
                }
            }
        }
        else if(dfn[e.to]<dfn[now]&&e.to!=pre){
            S.push(e);
            low[now]=min(low[now],dfn[e.to]);
        } 
    }
    if(pre<0&&son==1) iscut[now]=0;
}
void find_cut(int le,int ri){
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(iscut,0,sizeof(iscut));
    memset(bccno,0,sizeof(bccno));
    dfs_clock=bcc_cnt=0;
    for(int i=le;i<=ri;i++){
        if(!dfn[i]) tarjan(i,-1);
    }
}
 //  以上为求bcc模板 

int odd[MAXN]; // 点是不是在奇圈中 
int color[MAXN]; //染色用 。 1 为黑 2 为白色 
bool judge(int now,int num){
    for(int i=head[now];i!=-1;i=edge[i].nexts){
        Edge e=edge[i];
        if(bccno[e.to]!=num) continue; // 不在同一个bcc中 不用。 
        if(color[e.to]==color[now])  return 0;//颜色一样?  染色失败 
        if(!color[e.to]){
            color[e.to]=3-color[now];
            if(!judge(e.to,num)) return false;  // 之后的递归染色失败 
        }
    }
    return true; // 全部染色,成功 
}
void solve(){
    memset(odd,0,sizeof(odd));
    for(int i=1;i<=bcc_cnt;i++){
        memset(color,0,sizeof(color));
        for(int j=0;j<bcc[i].size();j++)  bccno[bcc[i][j]]=i;// 因为一个点可以再多个bcc中,要先更改一下。
        int now=bcc[i][0];
        color[now]=1;
        if(!judge(now,i)){ // 染色失败,不是二分图,有奇圈 ,其中所有的点都在奇圈中。 
            for(int j=0;j<bcc[i].size();j++) 
                odd[bcc[i][j]]=1;
        }
    }
    int ans=n;  
    for(int i=1;i<=n;i++) if(odd[i]) ans--;  // 统计不在奇圈中的个数 
    printf("%d\n",ans);
}
int main(){
    while(scanf("%d%d",&n,&m)&&(n||m)){
        init();
        getmap();
        find_cut(1,n);
        solve();
      }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值