poj 2942&&uva 3523 Knights of the Round Table(图的连通性)

来源:互联网 发布:linux自启动脚本编写 编辑:程序博客网 时间:2024/05/01 10:35
Knights of the Round Table
点击打开题目链接
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 9663 Accepted: 3129

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 51 41 52 53 44 50 0

Sample Output

2

Hint

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

Source

题目:

亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突,并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求:

1、  相互憎恨的两个骑士不能坐在直接相邻的2个位置;

2、  出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果。

如果出现有某些骑士无法出席所有会议(例如这个骑士憎恨所有的其他骑士),则亚瑟王为了世界和平会强制把他剔除出骑士团。

现在给定准备去开会的骑士数n,再给出m对憎恨对(表示某2个骑士之间使互相憎恨的),问亚瑟王至少要剔除多少个骑士才能顺利召开会议?

小知识:

求有向图的强连通分量(有向图中,如果一个顶点s到t有一条路径,t到s也有一条路径,即s与t互相可达,那么我们说s与t是强连通的。那么在有向图中,由互相强连通的顶点构成的分量,称作强连通分量。)的方法:

 kosaraju算法:第一步:在该图的逆图上运行DFS,将顶点按照后序编号的顺序放入一个数组中(显然,这个过程作用在DAG上得到的就是一个拓扑排序);第二步:在原图上,按第一步得出的后序编号的逆序进行DFS。也就是说,在第二次DFS时,每次都挑选当前未访问的结点中具有最大后序编号的顶点作为DFS树的树根。(详见博客:http://blog.sina.com.cn/s/blog_4dff87120100r58c.html       kosaraju算法 解释构造系统 http://www.93337.com/ism/cal_ring_use_kosaraju.php)

Tarjan算法:Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树。搜索时,把当前搜索树中未处理的节点加入一个堆栈,回溯时可以判断栈顶到栈中的节点是否为一个强连通分量。
定义DFN(u)为节点u搜索的次序编号(时间戳),Low(u)为u或u的子树能够追溯到的最早的栈中节点的次序号。
当DFN(u)=Low(u)时,以u为根的搜索子树上所有节点是一个强连通分量。(详见:http://hi.baidu.com/lydrainbowcat/item/1c664b662b1a1692c4d2491c   Tarjan算法解释构造系统:http://www.93337.com/ism/cal_ring_use_Tarjan.php)

Gabow算法:

Gabow算法与Tarjan算法的核心思想实质上是相通的,就是利用强连通分量必定是DFS的一棵子树这个重要性质,通过找出这个子树的根来求解强分量.具体到实现是利用一个栈S来保存DFS遇到的所有树边的另一端顶点,在找出强分量子树的根之后,弹出S中的顶点一一进行编号.二者不同的是,Tarjan算法通过一个low数组来维护各个顶点能到达的最小前序编号,而Gabow算法通过维护另一个栈来取代low数组,将前序编号值更大的顶点都弹出,然后通过栈顶的那个顶点来判断是否找到强分量子树的根(详见:http://www.nocow.cn/index.php/Gabow%E7%AE%97%E6%B3%95    Gabow算法解释构造系统:http://www.93337.com/ism/cal_ring_use_gabow.php)

另外关于三种算法可见:http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591370.html


关于图的基本知识:

点连通与边连通:

如果一个无向连通图的点连通度大于1,则称该图是点双连通的(point biconnected),简称双连通重连通。一个图有割点,当且仅当这个图的点连通度为1,则割点集合的唯一元素被称为割点(cut point),又叫关节点(articulation point)

如果一个无向连通图的边连通度大于1,则称该图是边双连通的(edge biconnected),简称双连通或重连通。一个图有桥,当且仅当这个图的边连通度为1,则割边集合的唯一元素被称为桥(bridge),又叫关节边(articulation edge)(摘自:https://www.byvoid.com/blog/biconnect/)

据说此题要用点连通,不能用边连通,还要判断奇圈,还要用染色法判断二分图(对于一个我这样刚刚接触图论不久的人来说,看到这里就已经晕了(>_<));

下面是《数据结构编程实验》上的解题思路:

先将图初始化为完全图,然后如果两个骑士之间互相憎恨,则,将他们的边删去,最后构造出一个图map[][],点代表骑士,边代表两个骑士之间是友好的;

low[u]数组是u及其子孙后代所能追溯到的最早的祖先v的时间序列pre[v],利用dfs(v)计算出图的块(双连通分量),并用ans储存块中的节点;

再判断当前块是否为欧拉图,若c块(双连通分量)为欧拉图的话,则其内的骑士全可以参加会议,将其ok[](标识变量设为真),最后计算ok值为假的人的数量即为最后结果

代码:

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;bool map[1024][1024];int pre[1024],low[1024];int ans[1024][1024],st[1024];int color[1024];bool flag,ok[1024];int n,cnt,sp,r;void dfs(int v)//递归求双连通分量{    st[sp++]=v;    pre[v]=low[v]=++cnt;//low[u]数组是u及其子孙后代所能追溯到的最早的祖先v的时间序列pre[v]    for(int i=0; i<n; i++)    {        if(map[v][i])        {            if(pre[i]==-1)            {                dfs(i);//递归                low[v]=min(low[i],low[v]);//找最早的祖先                if(low[i]>=pre[v])                {                    int k=0;                    st[sp]=-1;                    ans[r][0]=v;//r表示的是第r个双连通分量                    while(st[sp]!=i)                    {                        ans[r][++k]=st[--sp];//保存第r个连通分量的节点                    }                    ans[r][++k]=-1;                    if(k>2)                        r++;                }            }            else if(pre[i]<low[v])                low[v]=pre[i];        }    }}void dfs(int pnt,int c,int col){    if(flag)        return ;    for(int i=0; i<n; ++i)    {        if(map[c][i]&&color[i]!=0&&i!=pnt&&i!=c)        {            if(color[i]==1)            {                color[i]=col;                dfs(c,i,-col);            }            else if(color[i]==color[c])//类似于染色法            {                flag=true;                return ;            }        }    }}void slove(int c){    int now=0;    while(ans[c][now]!=-1)    {        color[ans[c][now]]=1;//将第c个连通分量中的节点的访问标识color置为1(未访问,0为节点非当前块,2为第奇数个访问的节点,-2为第偶数个访问的节点)        ++now;    }    flag=false;    color[ans[c][0]]=2;    dfs(-1,ans[c][0],-2);//计算骑士状态    now=0;    while(ans[c][now]!=-1)//恢复color的状态    {        color[ans[c][now]]=0;        if(flag)            ok[ans[c][now]]=true;        ++now;    }}int main(){    int m,i,a,b;    while(~scanf("%d%d",&n,&m)&&(n||m))    {        cnt=0;        memset(map,true,sizeof(map[0])*1024);        memset(pre,-1,sizeof(pre));        memset(st,0,sizeof(st));        memset(color,0,sizeof(color));        memset(ans,0,sizeof(ans[0])*1024);        memset(ok,false,sizeof(ok));        sp=r=0;        for(i=0; i<m; i++)//构建图        {            scanf("%d%d",&a,&b);            map[a-1][b-1]=0;            map[b-1][a-1]=0;        }        for(int i=0; i<n; i++)//计算双连通分量            if(pre[i]==-1)                dfs(i);        for(int i=0; i<r; i++)//计算骑士是否被驱逐的状态            slove(i);        int kick=0;        for(int i=0; i<n; ++i)        {            if(ok[i]==false)//如果要被驱逐计数                ++kick;        }        printf("%d\n",kick);//输出结果    }    return 0;}


个人认为这个不如判断是否为二分图,从而判断里面有无奇圈更使人明确些,因为题目要求必须是奇数个人才能坐在一起开会(可能这两个再做法上差不多),对于奇圈的判断,分析看的差不多,不知道如何弄;

附上几篇博客:http://blog.sina.com.cn/s/blog_7812e9860100wmo2.html

                http://blog.csdn.net/lyy289065406/article/details/6756821(这篇分析还可以)

                http://www.cnblogs.com/rainydays/archive/2012/11/04/2753623.html

0 0
原创粉丝点击