POJ2942-Knights of the Round Table (双联通+判断奇环)

36 篇文章 4 订阅


Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 13614 Accepted: 4561

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

Hint

Huge input file, 'scanf' recommended to avoid TLE. 

Source


思路转自:

题意:点击打开链接  http://blog.sina.com.cn/s/blog_d760eb2e0101dod3.html

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

有了上述知识支撑,可以开始解题了:

1、  利用m对憎恨对构造图G,则图G中有边相连的两个点表示这2个骑士互相憎恨。

2、  构造图G的补图~G,则图~G中有边相连的两个点表示这2个骑士可以坐在相邻位置。

3、  在图~G中,可能存在某些点的度数<=1,就是说这些骑士旁边最多只能坐另一个骑士,根据圆桌的座位要求每个骑士k的座位两边都必定各有一个骑士(k度数==2),那么我们认为这些度数<=1的点是孤立的或者是单连通的,也就是说他们不在圆桌的“环”内。

例如上图,我们利用图G构造补图~G后,显然骑士1的度=0,他是孤立的、不连通的;骑士5的度=1,他是单连通的;骑士{2,3,4}则构成一个双连通分量,他们正在圆桌“环”内开会。显然度数<=1的骑士1和骑士5都在环外,不满足出席会议的条件,亚瑟王为了维护世界和平自然会把这2人驱逐出骑士团。

 

4、  现在问题是,我们怎么才能知道哪些骑士在环外

我们可以把问题转化为,我们怎么才能知道哪些骑士在环内?显然在环内的所有结点都是双连通的,我们可以通过Tarjan算法求双连通分量。注意,补图~G可能有几个双连通子图,即它可能有不止一组双连通分量,而Tarjan算法是一组一组双连通分量求出来的,因此每求出一组双连通分量我们就要马上处理一组。

下面都是针对某一组双连通分量的处理。

 

5、  骑士在双连通分量内(在环内),并不能就此就说明它可以出席会议了,因为假如这个骑士所在的双连通分量,不是一个奇数顶点的环(奇圈),而是一个偶数顶点的环,那么这个双连通分量内的全部骑士都要被亚瑟王开除。

 

6、  那么怎样判断一个双连通分量是奇圈呢?

首先我们要接受两条定理,想知道证明过程的可以上网找,这里不证明:

(1)       如果一个双连通分量内的某些顶点在一个奇圈中(即双连通分量含有奇圈),那么这个双连通分量的其他顶点也在某个奇圈中;

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

由于双连通分量也是一个图,那么要判断双连通分量是否为奇圈,只需判断这个双连通分量是否为一个二分图,而要判断一个图是否为二分图,就用交叉染色法!

 

7、  显然所有在奇圈中的骑士,都是允许出席会议的,而由于可能有部分骑士允许出席一个以上的会议(即他们是2个以上的奇圈的公共点),那么为了避免重复统计人数,当我们判断出哪些骑士允许出席会议时,就把他们做一个标记(相同的骑士只做一个标记)。最后当Tarjan算法结束后,我们统计一下被标记的人数有多少,再用总人数减去这部分人,剩下的就是被亚瑟王剔除的人数了。


算法:双连通分量+二分图判定。

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

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

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

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

做出一个无向图,两名骑士之间存在边当且仅当这两名骑士可以相邻坐,所以一名骑士能参与圆桌会议的条件就是此图过这名骑士存在一个不重复经过一点的奇环,此处有一个结论,若存在一个奇环,则所有与此奇环存在公共边的环上的点都可选(此结论的证明不复杂,大家可自行脑补- -)。所以如果存在一个奇环,那么此与此奇环相关的点双联通分量都是可选的点


几个有用的结论;

(1)       如果一个双连通分量内的某些顶点在一个奇圈中(即双连通分量含有奇圈),那么这个双连通分量的其他顶点也在某个奇圈中;

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

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

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

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


总结:因为是一个环, 所以每个人两边必须有人, 也就是说不能是一条直线, 他给出了不相邻的, 我们没法操作,就找补图,找出他相邻的, 如果一个点所在的环全部都是偶环,那么他一定被踢,因为一个环没办法去除几个或者添加几个外面的点让这个图奇数,并且还是个环。。 这里的定理很多, 如果一个数再一个环上,他们必然是双联通, 如果一个双联通有奇环,那么整个环都在奇环上


代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 1e3 + 5;
const int maxe = maxn*maxn;
int Q[maxn];
int n, m, dfs_cnt, bcc_cnt, dfn[maxn], low[maxn], cut[maxn], bccno[maxn], book[maxn], color[maxn], mp[maxn][maxn];
vector<int> vt[maxn], bcc[maxn];
struct node
{
    int u, v;
    node(){}
    node(int uu, int vv) : u(uu), v(vv){}
}edge[maxe];
stack<node> sta;
void init()
{
    for(int i = 0; i < maxn; i++)
        vt[i].clear();
    memset(mp, 0, sizeof(mp));
    memset(book, 0, sizeof(book));
}
void tarjan(int x, int f)
{
    int son = 0;
    dfn[x] = low[x] = ++dfs_cnt;
    for(int i = 0; i < vt[x].size(); i++)
    {
        int to = vt[x][i];
        node e = node(x, to);
        if(!dfn[to])
        {
            sta.push(e), son++;
            tarjan(to, x);
            low[x] = min(low[x], low[to]);
            if(low[to] >= dfn[x])
            {
                cut[x] = 1;
                bcc[++bcc_cnt].clear();
                while(1)
                {
                    node t = sta.top();
                    sta.pop();
                    if(bccno[t.u] != bcc_cnt)
                    {
                        bcc[bcc_cnt].push_back(t.u);
                        bccno[t.u] = bcc_cnt;
                    }
                    if(bccno[t.v] != bcc_cnt)
                    {
                        bcc[bcc_cnt].push_back(t.v);
                        bccno[t.v] = bcc_cnt;
                    }
                    if(t.u == x && t.v == to)
                        break;
                }
            }
        }
        else if(dfn[to] < dfn[x] && to != f)
        {
            sta.push(e); //存边
            low[x] = min(low[x], dfn[to]);
        }
    }
    if(f < 0 && son == 1)
        cut[x] = 0;
}
int check(int s, int c)  //判断二分图
{
    int rear, frnt;
    rear = frnt = 0;
    memset(color, -1, sizeof(color));
    Q[rear++] = s;
    color[s] = 0;
    while(rear != frnt)
    {
        int u = Q[frnt++];
        for(int i = 0; i < vt[u].size(); i++)
        {
            int to = vt[u][i];
            if(bccno[to] != c) continue;
            if(color[to] == -1)
            {
                color[to] = !color[u];
                Q[rear++] = to;
            }
            else if(color[to] == color[u])  //不是二分图返回0
                return 0;
        }
    }
    return 1;
}
void get_bcc()
{
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(cut, 0, sizeof(cut));
    memset(bccno, 0, sizeof(bccno));
    dfs_cnt = bcc_cnt = 0;
    while(!sta.empty()) sta.pop();
    for(int i = 1; i <= n; i++)
    {
        if(!dfn[i])
            tarjan(i, -1);
//        if(check(i, bccno[i]))  //这样写不对的。。
//        {
//            for(int j = 1; j <= n; j++)
//            {
//                if(bccno[j] == bccno[i])
//                    book[j] = 1;
//            }
//        }
    }
}

int main()
{
    while(~scanf("%d%d", &n, &m), n+m)
    {
        init();
        int u, v;
        while(m--)
        {
            scanf("%d%d", &u, &v);
            mp[u][v] = mp[v][u] = 1;
        }
        for(int i = 1; i <= n; i++)
            for(int j = i+1; j <= n; j++)
                if(!mp[i][j]) vt[i].push_back(j), vt[j].push_back(i);
        get_bcc();
        for(int i = 1; i <= bcc_cnt; i++)  //把所有在一个联通分量的点染色成一个颜色,因为有点在多个连通分量
        {
            for(int j = 0; j < bcc[i].size(); j++)
                bccno[bcc[i][j]] = i;
            u = bcc[i][0];
            if(!check(u, i))
            {
                for(int j = 0; j < bcc[i].size(); j++)  //如果有奇数环,说明这个连通分量里的所有点都可以,都染色
                    book[bcc[i][j]] = 1;
            }
        }
        int ans = 0;
        for(int i = 1; i <= n; i++)
            if(!book[i]) ans++;
        printf("%d\n", ans);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值