★【双连通分量】【奇环判定】Knights of the Round Table

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

此题考察双连通分量,奇环判定以及补图的求法。
首先找出原图的补图,通过补图找点双连通分量,若该双连通分量上的点数大于一且为一个奇环,那么其中所有的点都不会被驱逐,否则所有点都被驱逐。
求点双连通分量的方法:(详见http://www.byvoid.com/blog/biconnect/
在求割点的过程中顺便把每个点双连通分支求出。建立一个栈,存储当前双连通分支,在搜索图时,把当前点加入栈中。如果遇到某时满足DFN[u] <= Low[v],说明u是一个割点,同时把点从栈顶一个个取出,直到遇到了点v,取出的这些边与其关联的点,组成一个点双连通分支。割点可以属于多个点双连通分支,其余点和每条边只属于且属于一个点双连通分支。
判定奇环的方法:对每个双连通分量进行DFS零壹染色,若冲突则为奇环,否则不为奇环。
Accode:

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>

const char fi[] = "knight.in";
const char fo[] = "knight.out";
const int maxN = 1010;

bool mp[maxN][maxN], marked[maxN], remain[maxN];
int DFN[maxN], Low[maxN], stack[maxN], col[maxN];
int Belong[maxN], Dcc[maxN];
int n, m, Dcnt, Ind, top, root;

void init_file()
{
    freopen(fi, "r", stdin);
    freopen(fo, "w", stdout);
    return;
}

void init_array()
{
    for (int i = 1; i < n + 1; ++i)
    for (int j = 1; j < n + 1; ++j)
        mp[i][j] = i != j;
    memset(DFN, 0, sizeof DFN);
    memset(Low, 0, sizeof Low);
    memset(Dcc, 0, sizeof Dcc);
    memset(Belong, 0, sizeof Belong);
    memset(marked, 0, sizeof marked);
    memset(remain, 0, sizeof remain);
	//注意数组清零,尽量将出现过的每一个数组都清零。
    return;
}

inline int getint()
{
    int res = 0; char tmp;
    while (!isdigit(tmp = getchar()));
    do res = (res << 3) + (res << 1) + tmp - '0';
    while (isdigit(tmp = getchar()));
    return res;
}

bool Dye(int u, int c)
{
    col[u] = c;
    for (int v = 1; v < n + 1; ++v)
    if (mp[u][v] && Belong[v] == Dcnt)
    if (col[u] == col[v] ||
        !col[v] && !Dye(v, -c))
        return 0;
    return 1;
}

inline bool odd(int u)
{
    memset(col, 0, sizeof col);
	//染色之前要将染色标记清零,再用1和-1交替进行染色。
    return !Dye(u, 1);
}

void tarjan(int u, int Last)
{
    DFN[u] = Low[u] = ++Ind;
    marked[stack[++top] = u] = 1;
    for (int v = 1; v < n + 1; ++v)
    if (mp[u][v] && v != Last)
    {
        if (!DFN[v])
        {
            tarjan(v, u);
            Low[u] = std::min(Low[u], Low[v]);
            if (DFN[u] <= Low[v])
		//注意这里把根也处理成一个割点,
		//即根也属于最后一个双连通分量。
            {
                ++Dcnt; int cnt = 0, tmp = v;
                do
                {
                    tmp = stack[top--];
                    Belong[tmp] = Dcnt;
                    marked[tmp] = 0;
                    Dcc[cnt++] = tmp;
                } while (tmp - v);
		//出栈时一定要遇到v就停。
                Dcc[cnt++] = u;
		//u也属于当前这个双连通分量。
                if (cnt > 1 && odd(u))
                for (int i = 0; i < cnt; ++i)
                    remain[Dcc[i]] = 1;
            }
        }
        else if (marked[v])
            Low[u] = std::min(Low[u], DFN[v]);
    }
    return;
}

void deal()
{
    while (m--)
    {
        int u = getint(), v = getint();
        mp[u][v] = mp[v][u] = 0;
    }
    Ind = Dcnt = top = 0; //标记清零。
    for (int i = 1; i < n + 1; ++i)
        if (!DFN[i]) tarjan(root = i, -1);
    int ans = 0;
    for (int i = 1; i < n + 1; ++i)
        if (!remain[i]) ++ans;
    printf("%d\n", ans);
    return;
}

int main()
{
    init_file();
    while (n = getint(), m = getint())
    {
        init_array();
        deal();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值