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

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. 

搜索双连通分量。深度优先搜索过程中,用一个栈保存所有经过的节点,判断割点,碰到割点就标记当前栈顶的结点并退栈,直到当前结点停止并标记当前割点。标记过的结点处于同一个双连通分量。

交叉染色搜索奇圈。在一个节点大于2的双连通分量中,必定存在一个圈经过该连通分量的所有结点;如果这个圈是奇圈,则该连通分量内的所有的点都满足条件;若这个圈是偶圈,如果包含奇圈,则必定还有一个奇圈经过所有剩下的点。因此一个双连通分量中只要存在一个奇圈,那么该双联通分量内的所有的点都处于一个奇圈中,在题目中,即武士可以坐成一圈。根据这个性质,只需要在一个双联通分量内找奇圈即可判断该双联通分量是否满足条件。交叉染色法就是在dfs的过程中反复交换着用两种不同的颜色对点染色,若某次dfs中当前结点的子节点和当前结点同色,则找到奇圈。

参考于:http://www.cnblogs.com/wuyiqi/archive/2011/10/19/2217911.html

#include "stdio.h"
#include "string.h" #define N 1010 int time;
int n,m;
bool map[N][N]; struct node
{
int x,y;
//int weight;
bool visit; //用来标记该边是否已经访问
int next;
}edge[2*N*N];
int idx,head[N]; bool odd[N];
bool mark[N]; //标记点是否为当前双连通分量中的元素
int low[N],dfn[N];
int st[N*N],top; //模拟栈
int col[N]; void Read_date();
inline int MIN(int a,int b) { return a<b?a:b; }
void Init(){ idx=0; memset(head,-1,sizeof(head)); }
void Add(int x,int y)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].visit = false;
edge[idx].next = head[x];
head[x] = idx++;
} bool find(int x) //判断当前双连通分量是否为二分图
{
int i,y;
for(i=head[x]; i!=-1; i=edge[i].next)
{
y = edge[i].y;
if(mark[y])
{
if(col[y]==-1)
{
col[y] = !col[x];
return find(y);
}
else if(col[y]==col[x])
return false; //不是二分图,返回false
}
}
return true; //是二分图,返回true
} void Color(int x)
{
int i;
memset(mark,false,sizeof(mark));
while(1)
{
i = st[top];
top--;
mark[edge[i].x] = true;
mark[edge[i].y] = true;
if(edge[i].x==x) break;
}
memset(col,-1,sizeof(col));
col[x] = 0;
if(!find(x)) //双连通分量不是二分图,则这些点全部可以
{
for(i=1; i<=n; ++i)
{
if(mark[i])
odd[i] = 1;
}
}
} void DFS(int x)
{
int i,y;
low[x] = dfn[x] = ++time;
for(i=head[x]; i!=-1; i=edge[i].next)
{
y = edge[i].y;
if(edge[i].visit) continue;
edge[i].visit = edge[i^1].visit = 1;
st[++top] = i;
if(!dfn[y])
{
DFS(y);
low[x] = MIN(low[x],low[y]);
if(low[y]>=dfn[x]) //找到割顶或者为根节点
Color(x);
}
else
low[x] = MIN(low[x],dfn[y]);
}
} int Solve()
{
int i;
int num=0;
time = 0;
top = 0;
memset(dfn,0,sizeof(dfn));
memset(odd,false,sizeof(odd));
for(i=1; i<=n; ++i)
{
if(!dfn[i]) //表示点i未被访问过
DFS(i); //以i为根节点找双连通分量
}
for(i=1; i<=n; ++i)
{
if(!odd[i])
num++;
}
return num;
} int main()
{
while(scanf("%d%d",&n,&m),n||m)
{
Read_date();
printf("%d\n",Solve());
}
return 0;
} void Read_date()
{
int i,j;
int x,y;
memset(map,true,sizeof(map));
while(m--)
{
scanf("%d %d",&x,&y);
map[x][y] = map[y][x] = false;
}
Init();
for(i=1; i<=n; ++i)
{
for(j=i+1; j<=n; ++j)
{
if(map[i][j])
{
Add(i,j);
Add(j,i);
}
}
}
}

poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)的更多相关文章

  1. POJ 2942 Knights of the Round Table (点双连通分量)

    题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...

  2. poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)

    #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...

  3. POJ 2942 Knights of the Round Table 黑白着色+点双连通分量

    题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...

  4. POJ2942 UVA1364 Knights of the Round Table 圆桌骑士

    POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...

  5. poj 2942 Knights of the Round Table - Tarjan

    Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...

  6. POJ 2942 Knights of the Round Table

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10911   Acce ...

  7. POJ 2942 Knights of the Round Table - from lanshui_Yang

    Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels ...

  8. 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)

    Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...

  9. poj 2942 Knights of the Round Table(点双连通分量+二分图判定)

    题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...

随机推荐

  1. AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. f4: Facebook’s Warm BLOB Storage System——Erasure Code

    Facebook在OSDI 2014上发表论文f4: Facebook's Warm BLOB Storage System,这个系统主要目的就是降低存储成本,在容忍磁盘,主机,机架,数据中心的同时提 ...

  3. 个人学习对UIView动画的总结

    我的博客之前已经开通五个月了,但是一直没有写东西.一是不敢写,二是也不知道写啥.毕竟是一个刚刚入行大半年的菜鸟,现在总想通过各种办法提高自己.之前总感觉用到一些东西,只是当时搞懂了一点,加上并没有总结 ...

  4. ActiveReports 报表应用教程 (2)---清单类报表

    在大多报表系统中都有清单类报表的身影,比如:客户清单.商品信息清单.设备清单.物品采购清单.记账凭证.货品发货清单.员工清单等等.清单类报表看视乎比较简单,但是,由清单类报表演变而来的报表类型却十分丰 ...

  5. 几个gcc的扩展功能

    -finstrument-functions  constructor   destructor __builtin_return_address http://linuxgazette.net/15 ...

  6. 你还记的那一年你我学习的-->>用表组织数据*(数据表)

    不知不觉,踏上IT之路,光阴似箭,日月如梭.虽好像回到从前,回到那个无忧无虑的童年,回到那个花样少年的青春;回到那个年少幼稚的小学;回到那个整天幻想的初中;回到那个顽强不屈,誓死不弃的高中;回到那个整 ...

  7. ahjesus 部署lighttpd

    这个就不写了,直接传送门过去看,按照说的做就可以了 如果你想要安装最新版的,传送门 需要注意的是configure这一步,你看完他的help以后还要输入 ./configure 才能继续下一步 再就是 ...

  8. Python迭代器:捕获Generator的返回值

    但是用for循环调用generator时,发现拿不到generator的return语句的返回值.如果想要拿到返回值,必须捕获StopIteration错误,返回值包含在StopIteration的v ...

  9. mysql max_allowed_packet过小导致的prepare失败

    最近公司一台阿里云上模拟环境突然好好地就出错了额,总提示:"Unknown prepared statement handler (stmt) given to DEALLOCATE PRE ...

  10. Linux编辑器vim键盘详解

    下面的这张图,一看就明白了,从此,学习变的不再艰难! 补注:图中没有关于查找和替换的,应该用下面的.自上而下的查找操作                  /word小写的n和N自下而上的查找操作    ...