Knights of the Round Table
Knights of the Round Table
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.
0
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.
solution
不相互憎恨的骑士连边
问题变成求不在任何一个简单奇圈上的点的个数(奇圈可以开会)
如果 某个点双连通分量中存在奇环,则该点双联通分量中所有点都在某个奇环内
傻了。。。
1 2
1 3
2 3
是点双。。。。。 
染色判环即可
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define maxn 1002
#define M 2000006
using namespace std;
int n,m,t1,t2,head[maxn],fl[maxn][maxn],tot;
int dfn[maxn],low[maxn],zh[M],top,sc,cnt,he[maxn],tt;
int flag[maxn],co[maxn],fsy[maxn];
struct node{
	int nex,u,v;
}e[M],h[M];
void lj(int t1,int t2){
	e[++tot].v=t2;e[tot].u=t1;e[tot].nex=head[t1];head[t1]=tot;
}
void add(int t1,int t2){
	h[++tt].v=t2,h[tt].nex=he[t1];he[t1]=tt;
}
void lian(int u,int v){
	cnt++;
	while(top>0){
		t1=e[zh[top]].u,t2=e[zh[top]].v;
		add(cnt,t1);add(cnt,t2);
		if(t1==u&&t2==v){top--;break;}
		top--;
	}
}
void tarjan(int k,int fa){
	dfn[k]=low[k]=++sc;
//	cout<<k<<' '<<dfn[k]<<endl;
	for(int i=head[k];i;i=e[i].nex){
		//cout<<"fsy "<<e[i].v<<endl;
		if(e[i].v==fa)continue;
		if(!dfn[e[i].v]){
			zh[++top]=i;
			tarjan(e[i].v,k);
			low[k]=min(low[k],low[e[i].v]);
			if(low[e[i].v]>=dfn[k])lian(k,e[i].v);//geding
		}
		else{
			if(low[k]>dfn[e[i].v]){
				low[k]=dfn[e[i].v];
				zh[++top]=i;
			}
		}
	}
//	cout<<"aa "<<k<<' '<<low[k]<<endl;
}
bool pd(int k){
	for(int i=head[k];i;i=e[i].nex){
		if(!flag[e[i].v])continue;
		if(!co[e[i].v]){
			co[e[i].v]=3-co[k];
			if(!pd(e[i].v))return 0;
		}
		else if(co[e[i].v]!=3-co[k])return 0;
	}
	return 1;
}
void Q()
{
	sc=tot=tt=0;
	for(int i=1;i<=1000;i++)
	head[i]=he[i]=dfn[i]=low[i]=fsy[i]=0;
	memset(fl,0,sizeof fl);
	memset(e,0,sizeof e);memset(h,0,sizeof h);
}
int main(){
	while(~scanf("%d%d",&n,&m)&&n){
		Q();
		for(int i=1;i<=m;i++){
			scanf("%d%d",&t1,&t2);
			fl[t1][t2]=fl[t2][t1]=1;
		}
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			if(!fl[i][j]&&i!=j)lj(i,j);
		}
		for(int i=1;i<=n;i++){
			if(!dfn[i])tarjan(i,0);
		}
		for(int x=1;x<=cnt;x++){
			memset(flag,0,sizeof flag);
			memset(co,0,sizeof co);
			for(int i=he[x];i;i=h[i].nex)flag[h[i].v]=1;
			//cout<<x<<endl;
			//for(int i=he[x];i;i=h[i].nex)cout<<h[i].v<<' ';cout<<endl;
			int S=h[he[x]].v;co[S]=1;
			if(!pd(S)){
				for(int i=he[x];i;i=h[i].nex)fsy[h[i].v]=1;
			}//youjihuan  keyicanjia
		}
		int ans=n;
		for(int i=1;i<=n;i++)ans-=fsy[i];
		cout<<ans<<endl;
	}
	return 0;
}												
											Knights of the Round Table的更多相关文章
- 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
		
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
 - poj 2942 Knights of the Round Table  圆桌骑士(双连通分量模板题)
		
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
 - 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
		
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
 - 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 ...
 - UVALive - 3523 - Knights of the Round Table
		
Problem UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...
 - 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 ...
 - 【POJ】2942 Knights of the Round Table(双连通分量)
		
http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...
 - POJ 2942 Knights of the Round Table 黑白着色+点双连通分量
		
题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...
 - [POJ2942][LA3523]Knights of the Round Table
		
[POJ2942][LA3523]Knights of the Round Table 试题描述 Being a knight is a very attractive career: searchi ...
 
随机推荐
- cd ..和cd -
			
cd ..是返回上一层目录, cd -是返回到上一次的工作目录.
 - 支持无限加载的js图片画廊插件
			
natural-gallery-js是一款支持无限加载的js图片画廊插件.该js图片画廊支持图片的懒加载,可以对图片进行搜索,分类,还可以以轮播图的方式来展示和切换图片. 使用方法 在页面中引入下面的 ...
 - java 代码优化
			
Java程序中的内存管理机制是通过GC完成的,“一个对象创建后被放置在JVM的堆内存中,当永远不在应用这个对象的时候将会被JVM在堆内存中回收.被创建的对象不能再生,同时也没有办法通过程序语句释放”( ...
 - 稍微深入点理解C++复制控制【转】
			
通过一个实例稍微深入理解C++复制控制过程,参考资料<C++ primer>,介绍点基本知识: 1.在C++中类通过特殊的成员函数:复制构造函数.赋值操作符和析构函数来控制复制.赋值和撤销 ...
 - 一步一步讲解安装NodeJs开发环境
			
node.js简介 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node. ...
 - JS - Object.create(prototype)方法
			
用Object.create(prototype)方法创建一个对象,这个对象的原型将指向这个传入的prototype参数
 - ajax400错误
			
在用ajax向后台传递参数时,页面一直显示错误400 bad request. 出现这个问题的原因是,要传递的VO类里一个实体bean里面的两个字段名称与前台表单序列化之后的name名称不匹配. 解决 ...
 - Shell脚本使用汇总整理——达梦数据库备份脚本
			
Shell脚本使用汇总整理——达梦数据库备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/9223477.html 脚本 ...
 - .pyc是什么鬼
			
.pyc是个什么鬼? 1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存 ...
 - Java中的finally
			
基础用法: int f1() { try{ return 1; }finally { System.out.println("finall执行"); } } @Test publi ...