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

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
题意:一群骑士要召开圆桌会议,坐在圆桌上的人都有两个邻居
满足两个要求:每个人与身边的邻居都不能有矛盾,且围坐的人数必须是奇数
题目给出m对矛盾关系,问有多少人一定是不能参加会议的
分析:
1.首先做补图,把题中给出的边舍去,把没有给出的边建图
2.然后在图中找奇数环,求点的双连通块
3.在双连通块中找奇数点的环,如果一个联通块中存在一个奇数环,那么该连通块中的每个人都有可能成为一个奇数环中的一员,此时把可能成为一员的人标记
4.找奇数环实际上就是判断该联通块是否是二分图,即用黑白交叉染色法判断,如果某条边的两个端点有相同的颜色,就存在了奇数环
程序:
 #include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 1009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
#include"stack"
#define LL __int64
using namespace std;
struct node
{
int u,v,next;
}edge[M*M*];
int t,head[M],dfn[M],low[M],cut[M],belong[M*M*],use[M],color[M],exist[M];
int indx,num;
int G[M][M];
stack<int>q;
vector<int>bian[M];
void init()
{
t=;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
int dfs(int u)//交叉染色判断二分图
{
for(int i=;i<(int)bian[u].size();i++)
{
int v=bian[u][i];
if(color[v]==-)
{
color[v]=color[u]^;
int tt=dfs(v);
if(tt)
return ;
}
else if(color[v]==color[u])
return ;
}
return ;
}
void tarjan(int u,int fa,int id)
{
dfn[u]=low[u]=++indx;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(i==(id^))continue;
if(!dfn[v])
{
q.push(i);//把边入栈
tarjan(v,u,i);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v])//求割点
{
cut[u]++;
int x;
num++;
int cnt=;
map<int,int>mp;
do//当搜到一个割点u的时候把栈中边出栈,所有出栈的边都属于一个点双连通块
{
x=q.top();
q.pop();
belong[x]=belong[x^]=num;
bian[edge[x].u].push_back(edge[x].v);//建立连通块联通图
bian[edge[x].v].push_back(edge[x].u);
if(!mp[edge[x].u])//离散化点
{
mp[edge[x].u]=;
use[cnt++]=edge[x].u;
}
if(!mp[edge[x].v])
{
mp[edge[x].v]=;
use[cnt++]=edge[x].v;
} }while(i!=x);
color[use[]]=;
if(dfs(use[]))//如果存在奇数环,把点标记
{
for(int i=;i<cnt;i++)
exist[use[i]]=;
}
for(int i=;i<cnt;i++)//清理初始化边和color标记
{
bian[use[i]].clear();
color[use[i]]=-;
}
}
}
else if(low[u]>dfn[v])
{
low[u]=dfn[v];
q.push(i);//把边入栈
}
}
if(fa<)
cut[u]--;
}
int slove(int n)
{
for(int i=;i<=n;i++)
bian[i].clear();
indx=num=;
memset(cut,,sizeof(cut));
memset(dfn,,sizeof(dfn));
memset(use,,sizeof(use));
memset(color,-,sizeof(color));
memset(exist,,sizeof(exist));
for(int i=;i<=n;i++)
{
if(!dfn[i])
tarjan(i,-,-);
}
int ans=;
for(int i=;i<=n;i++)
{
if(!exist[i])
ans++;
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),m||n)
{
init();
memset(G,,sizeof(G));
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[a][b]=G[b][a]=;
}
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
if(!G[i][j])
{
add(i,j);
add(j,i);
}
}
}
int ans=slove(n);
printf("%d\n",ans);
}
return ;
}
 

点的双联通+二分图的判定(poj2942)的更多相关文章

  1. poj2942 点-双联通+二分图染色

    题意:有一群骑士要坐在一个圆形的桌子上,他们之间有些人相互讨厌,所以不能挨着,要求算出一次也不能坐在桌子上的人,每次会议桌子必须奇数个人,一个人不能开会 题解:可以先建一个补图,要满足题目条件我们只要 ...

  2. poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图

    http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011 ...

  3. poj2942 Knights of the Round Table,无向图点双联通,二分图判定

    点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...

  4. poj2942(双联通分量,交叉染色判二分图)

    题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 思路:首先 ...

  5. Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定

    题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...

  6. POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】

    LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...

  7. 【POJ2942】Knights of the Round Table(二分图 点双联通分量)

    题目链接 大意 给定\(N\)个点与\(M\)个关系,每个关系表示某两个点间没有直接的边相连,求不在所有奇环上的点的个数. (\(1\le N\le 1e3,1\le M\le 1e6\)) 思路 考 ...

  8. 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)

    layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...

  9. 双栈排序(洛谷P1155)二分图的判定+思维贪心

    题目:戳这里 题目大意: 给你一个数列,问能否通过两个栈的push与pop把它输出成一个升序序列(每个数只能入队并出队一次) 不能的话输出0,能的话输出操作方法 主要思路: 1.判断是否可以成功输出升 ...

随机推荐

  1. JS基础语法

    1.注释语法://单行注释./*多行注释*/. 2.输出语法:{1.alert("要输出的字符串"):.alert(输出其类型):  2.confirm():弹出一个可以和用户交互 ...

  2. 【转】设计模式 ( 十七) 状态模式State(对象行为型)

    设计模式 ( 十七) 状态模式State(对象行为型) 1.概述 在软件开发过程中,应用程序可能会根据不同的情况作出不同的处理.最直接的解决方案是将这些所有可能发生的情况全都考虑到.然后使用if... ...

  3. 利用 libiconv 实现汉字编码 utf-8 格式 和 gbk格式的相互转换

    参考文章:http://jimmee.iteye.com/blog/2174693 关于windows上编译libiconv的库,请参见:http://www.cnblogs.com/tangxin- ...

  4. C++ 编译器内存错误 after Normal block。。。

    解决 after Normal block(#908) at 0x399EC0. CRT detected that the application wrote to memory after end ...

  5. about eclipse

    导出javadoc command选择bin下的javadoc.exe 配置编码和文字 英文:-locale en_US -encoding UTF-8 -charset UTF-8 中文:-loca ...

  6. ArcGIS中如何导出单个矢量要素图形

    原文:ArcGIS中如何导出单个矢量要素图形 在ARCGIS中载入了一张含有省界的中国地图,是SHP文件.现在我只想要其中一块地区的,实现方法如下: 加入到ArcGIS后,右击图层,打开属性表(att ...

  7. Swift-10--错误处理

    如何优雅地抛出错误-- 抛出错误使用throw关键字. 某个错误被抛出时,那个地方的某部分代码必要要负责处理这个错误,比如纠正这个问题.尝试另外一种方式.或是给用户提示这个错误. ***4种处理错误的 ...

  8. Excel 的一些用法--行号赋给一列

    1.鼠标选定要赋值的列 2.在 输入 =Row() 3. Ctrl+Enter

  9. git还原成某个点

    1.本地 git  reset  --hard    commit值 git  push  -f   // 强制同步到git库(git服务器) 2.项目所在线上服务器 git  reset  --ha ...

  10. Ioc-Autofac实现自动的注入

    在开发过程中,最郁闷的莫过于当你新增一个Service时,你需要对该Service进行注册,有的是使用代码注入,有的是XML配置注入,不管是哪种类型的注入,经常会出现开发人员忘记注入的情况. 于是我试 ...