Knights of the Round Table-POJ2942(双连通分量+交叉染色)
Description
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
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
Sample Input
5 5
1 4
1 5
2 5
3 4
4 5
0 0
Sample Output
2
Hint
Source
(1):搜索双连通分量。DFS过程中,用一个栈保存所有经过的点,判断割点,碰到割点就将标记栈的顶点并退栈,直到当前节点停止标记当前割点。标记过的节点在同一个连通分量里。
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define LL long long using namespace std; const int Max = 1100; typedef struct Node
{
int num; int next;
}Line; Line Li[Max*100]; int top; int Head[Max]; int low[Max],dfn[Max]; int Map[Max][Max]; int n,m; int Num,num,ant; int Que[Max]; int vis[Max]; int part[Max],color[Max]; void AddNum(int u)
{
Li[top].num=num;
Li[top].next = Head[u];
Head[u] = top++;
} void dfs(int u,int father)
{
vis[u]=1;//表示正在访问 dfn[u]=low[u]=Num++; Que[ant++]=u; for(int i=1;i<=Map[u][0];i++)
{
if(vis[Map[u][i]]==1&&Map[u][i]!=father)
{
low[u]=min(low[u],dfn[Map[u][i]]);
}
if(vis[Map[u][i]]==0)
{
dfs(Map[u][i],u); low[u]=min(low[Map[u][i]],low[u]); if(low[Map[u][i]]>=dfn[u])//子树形成环
{
// 标记
AddNum(u); for(int j=Que[ant];j!=Map[u][i];AddNum(j))
{
j=Que[--ant];
} num++ ;
}
}
} vis[u]=2;//表示已经访问并且处理完
} int OddCycle(int u,int flag)//判断奇环
{
color[u]=flag; for(int i=1;i<=Map[u][0];i++)
{
if(!part[Map[u][i]])
{
continue;
} if(color[Map[u][i]]==0 && OddCycle(Map[u][i],-flag))
{
return 1;
}
if(color[Map[u][i]]==flag)
{
return 1;
}
}
return 0;
} int main()
{
int u,v; while(~scanf("%d %d",&n,&m)&&(n+m))
{ // 数据处理
memset(Map,0,sizeof(Map)); for(int i=1;i<=m;i++)
{
scanf("%d %d",&u,&v); Map[u][v]=Map[v][u]=1;
} for(int i=1;i<=n;i++)
{
Map[i][i]=1;
}
//建图
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
Map[0][j]=Map[i][j];
} for(int j=1;j<=n;j++)
{
if(!Map[0][j])
Map[i][++Map[i][0]]=j;
}
} memset(vis,0,sizeof(vis)); // 标记是否被访问,以及访问的状态 memset(Head,-1,sizeof(Head)); Num = 0; num=0 ; top=0;ant = 0; //去除割边 for(int i=1;i<=n;i++)
{
if(!vis[i])
{
Num = 0;
dfs(i,0);
}
} memset(vis,0,sizeof(vis)); for(int i=0;i<num;i++)//判断奇环
{ memset(part,0,sizeof(part)); memset(color,0,sizeof(color)); for(int j=1;j<=n;j++)
{
for(int k=Head[j];k!=-1;k=Li[k].next)
{
if(Li[k].num == i)
{
part[j]=1;
break;
}
}
} for(int j=1;j<=n;j++)
{
if(part[j])
{
if(OddCycle(j,1))
{
for(int k=1;k<=n;k++)
{
vis[k]+=part[k];
}
}
break;
}
}
} int ans = 0; for(int i=1;i<=n;i++)
{
if(!vis[i])
{
ans++;
}
} printf("%d\n",ans);
}
return 0;
}
Knights of the Round Table-POJ2942(双连通分量+交叉染色)的更多相关文章
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
		
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
 - 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)
		
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
 - 【POJ】2942 Knights of the Round Table(双连通分量)
		
http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...
 - POJ2942   Knights of the Round Table      点双连通分量,逆图,奇圈
		
题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先 ...
 - POJ2942 Knights of the Round Table 点双连通分量 二分图判定
		
题目大意 有N个骑士,给出某些骑士之间的仇恨关系,每次开会时会选一些骑士开,骑士们会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件:1.任意相互憎恨的两个骑士不能相邻.2.开会人数为大于2的奇 ...
 - poj 2942 Knights of the Round Table(点双连通分量+二分图判定)
		
题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...
 - UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)
		
题目大意:有n个骑士要在圆桌上开会,但是相互憎恶的两个骑士不能相邻,现在已知骑士们之间的憎恶关系,问有几个骑士一定不能参加会议.参会骑士至少有3个且有奇数个. 题目分析:在可以相邻的骑士之间连一条无向 ...
 - POJ 2942 Knights of the Round Table(双连通分量)
		
http://poj.org/problem?id=2942 题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...
 - [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)
		
建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点 ...
 
随机推荐
- java final方法的作用
			
1. 不想让别人修改方法实现. 2. 在方法调用时使用内嵌调用. 3. 有效的“关闭”动态绑定,这样编译器就可以为final方法调用生成更有效的代码. Java编程思想: “然而,大多数情况下,这样做 ...
 - HashMap实现原理分析
			
1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1 ...
 - win7安装virtualbox遇到的问题
			
今天用台式机的时候想装个virtualbox跑centos做测试用,结果centos始终装不上,vbox一直提示无法开启任务.重装vbox,以及手动点击安装xxx.inf文件,都不行. 以前用的win ...
 - Code Snippet
			
Code Snippet: http://msdn.microsoft.com/en-us/library/z41h7fat.aspx CodePlex.Snippets 4.0 - Visual S ...
 - Java  隐藏和覆盖
			
我们知道,在JAVA中,子类可以继承父类,如果子类声明的方法与父类有重名的情况怎么办,大伙儿都知道要是重写,但是实际上这又分为两种情况,就是方法和变量在继承时的覆盖和隐藏问题,这些概念性的东西看似无聊 ...
 - 【java基础学习】字符串
			
字符串 1. java内存区域(堆区.栈区.常量池) 2. String方法 获取长度 length(); 获取位置 indexOf(index); lastIndexOf(index) 获取子串 c ...
 - String类中一些的方法的应用
			
一.整理string类 1.Length():获取字串长度: 2.charAt():获取指定位置的字符: 3.getChars():获取从指定位置起的子串复制到字符数组中:(它有四个参数) 4.rep ...
 - iOS CAShapeLayer记录
			
基本知识 看看官方说明: /* The shape layer draws a cubic Bezier spline in its coordinate space. * * The spline ...
 - MySQL 范式
			
在做笔试题的时候遇到一些判断范式的题,就去找了些博客与书来看,觉得这个是比较好理解的: 第一范式:确保每列的原子性. 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足 ...
 - JMeter学习-004-WEB脚本入门实战
			
此文为 JMeter 入门实战实例.我是 JMeter 初学菜鸟一个,因而此文适合 JMeter 初学者参阅.同时,因本人知识有限,若文中存在不足的地方,敬请大神不吝指正,非常感谢! 闲话少述,话归正 ...