【连通图|双连通+二分图判定】POJ-2942 Knights of the Round Table

34 篇文章 0 订阅
19 篇文章 0 订阅
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 is there, 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. 
————————————————————————————————————————————————————————————————————————————————————————————————
前言:题意有一点不太清楚,就是可以有多张桌子。
思路:左右邻居都不能有仇恨。
如果以没有仇恨为边建图。假设为A、B、C三个人坐在一起,从某个点X是可以通过A这条路到达B,也可以通过C到达B,路上没有敌对关系。
也就是双连通分量的定义,任意两点存在两条互为独立轨的路径。
Tarjan算法求出双连通分量之后,要求出“被踢走的骑士”,也就是不在任何一个双连通分量中的点的个数。
但是题目还要求,必须是奇数个人,因此要找的是“不在任何一个简单奇圈”上的点的个数。
对于一个双连通分量:首先我们知道二分图一定是不含奇圈的。那么对于一个不是二分图的双连通分量,它的每一个顶点是否一定属于一个奇圈呢?
答案是肯定的。不是二分图,就含有至少一个奇圈。如果某个点v恰好属于一个偶圈,那么这个偶圈和那个奇圈就构成一个奇圈。
因此只需要储存每个连通分量(另外你还要储存每个点属于哪个连通分量),遍历他们判断是否是二分图(奇偶染色法),如果不是,就标记每个点为“在奇圈上”,最后没在奇圈上的点就是答案。
代码如下:
/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define Mem(f, x) memset(f, x, sizeof(f))
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 1e3+5, M = 1e6+5;
struct Pair {
    int u, v;
    Pair(){}
    Pair(int _u, int _v):
        u(_u), v(_v){}
};
struct Edge {
    int v, next;
    Edge(){}
    Edge(int _v, int _next):
        v(_v), next(_next){}
}e[M];
int n, m, tot, deep, bcc_cnt;
int dfn[N], head[N], iscut[N], bcc_id[N], color[N];
bool G[N][N], odd[N];
stack <Pair> s;
vector <int> bcc[N];

void __init__()
{
    Mem(G, 0);
    for(int i = 0;i < n; i++) {
        head[i] = -1;
        odd[i] = dfn[i] = iscut[i] = bcc_id[i] = 0;
    }
    deep = tot = bcc_cnt = 0;
}

void add(int u, int v)
{
    e[tot] = Edge(v, head[u]);
    head[u] = tot++;
}

int dfs(int u, int fa)
{
    int lowu = dfn[u] = ++deep;
    int son = 0;
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        Pair p = Pair(u, v);
        if(!dfn[v]) {
            s.push(p);
            son++;
            int lowv = dfs(v, u);
            lowu = min(lowu, lowv);
            if(lowv >= dfn[u]) {
                iscut[u] = 1;
                bcc_cnt++;//颜色是从1开始编号的
                bcc[bcc_cnt].clear();
                while(1) {
                    Pair x = s.top(); s.pop();
                    if(bcc_id[x.u] != bcc_cnt) {
                        bcc[bcc_cnt].PB(x.u); 
                        bcc_id[x.u] = bcc_cnt;
                    }
                    if(bcc_id[x.v] != bcc_cnt) {
                        bcc[bcc_cnt].PB(x.v);
                        bcc_id[x.v] = bcc_cnt;
                    }
                    if(x.u == u && x.v == v) break;
                }
            }
        }
        else if(dfn[v] < dfn[u] && v != fa) {
            s.push(p);
            lowu = min(lowu, dfn[v]);
        }
    }
    if(fa == -1 && son == 1) iscut[u] = 0;
    return lowu;
}

bool erfen(int u, int c)
{//0:未经染色 1:起点颜色 2:交错点颜色
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(bcc_id[v] == c) {
            if(color[v] == color[u]) return false;
            if(!color[v]) {
                color[v] = 3 - color[u];
                if(!erfen(v, c)) return false;//false的优先级高于true
            }
        }
    }
    return true;
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    while(scanf("%d%d", &n, &m), n||m) {
        int u, v;
        __init__();
        for(int i = 0; i < m; i++) {
            scanf("%d%d", &u, &v);
            u--; v--;
            G[u][v] = G[v][u] = 1;
        }//为没有冲突的人建边
        for(int i = 0; i < n; i++) {
            for(int j = i+1; j < n; j++) {
                if(!G[i][j]) {
                    add(i, j);
                    add(j, i);
                }
            }
        }
        for(int i = 0; i< n; i++) {
            if(!dfn[i]) dfs(i, -1);
        }//为每个连通图跑一次tarjan
        for(int i = 1; i <= bcc_cnt; i++) {
            int ne = bcc[i].size();
            for(int j = 0; j < ne; j++) {
                bcc_id[bcc[i][j]] = i;//为每个双连通分量重新染色以确保割顶的颜色
            }//因为割顶会被多次着色
            int u = bcc[i][0];//起点
            Mem(color, 0);
            color[u] = 1;
            if(!erfen(u, i)) {
                for(int j = 0; j < ne; j++) {
                    odd[bcc[i][j]] = 1;//如果不是二分图,那么一定处在奇圈上
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < n; i++) {
            if(!odd[i]) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值