点的双联通+二分图的判定(poj2942)
| Time Limit: 7000MS | Memory Limit: 65536K | |
| Total Submissions: 10804 | Accepted: 3553 |
Description
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 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
题意:一群骑士要召开圆桌会议,坐在圆桌上的人都有两个邻居
满足两个要求:每个人与身边的邻居都不能有矛盾,且围坐的人数必须是奇数
题目给出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)的更多相关文章
- poj2942 点-双联通+二分图染色
题意:有一群骑士要坐在一个圆形的桌子上,他们之间有些人相互讨厌,所以不能挨着,要求算出一次也不能坐在桌子上的人,每次会议桌子必须奇数个人,一个人不能开会 题解:可以先建一个补图,要满足题目条件我们只要 ...
- poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图
http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011 ...
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...
- poj2942(双联通分量,交叉染色判二分图)
题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 思路:首先 ...
- Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定
题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...
- POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】
LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...
- 【POJ2942】Knights of the Round Table(二分图 点双联通分量)
题目链接 大意 给定\(N\)个点与\(M\)个关系,每个关系表示某两个点间没有直接的边相连,求不在所有奇环上的点的个数. (\(1\le N\le 1e3,1\le M\le 1e6\)) 思路 考 ...
- 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)
layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...
- 双栈排序(洛谷P1155)二分图的判定+思维贪心
题目:戳这里 题目大意: 给你一个数列,问能否通过两个栈的push与pop把它输出成一个升序序列(每个数只能入队并出队一次) 不能的话输出0,能的话输出操作方法 主要思路: 1.判断是否可以成功输出升 ...
随机推荐
- Artificial Intelligence Research Methodologies 人工智能研究方法
Computer Science An Overview _J. Glenn Brookshear _11th Edition To appreciate the field of artificia ...
- Android 加入一个动作按钮
在XML中声明一个动作按钮 所有的动作按钮和其他的可以利用的items都定义在menu资源文件夹中的XML文件中.为了增加一个动作按钮到工具栏,需要在工程 /res/menu/ 目录下面创建一个新的X ...
- 小米2/2S 手机由 Smartisan OS ROM 刷回 MIUI 教程
刷机所需文件1.请前往 MIUI 官网下载小米 2/2S 标准版 MIUI ROM 包.下载地址:http://www.miui.com/getrom.php?r=2:下载后请将原文件名修改为“upd ...
- 【No.3 Ionic】超级逗表情 App
本人使用Ionic框架开发了一个 超级逗表情 的App 依赖插件 cordova-plugin-app-version 0.1.9 "AppVersion" cordova-plu ...
- xcode 路径
$(SRCROOT)宏和$(PROJECT_DIR)宏 XCode环境变量及路径设置 分类: Objective-C2013-03-11 12:30 41336人阅读 评论(1) 收藏 举报 一般我们 ...
- gtd好文两篇收藏
http://www.jianshu.com/p/bf5e8a9761f5 http://blog.sina.com.cn/s/blog_4e0f66b80100m73i.html
- 欢迎大家提问Android技术及职业生涯等问题
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 最近有些时间,但QQ群问的问题比较多,不能一一解答,如果有价值 ...
- Linux下编译静态MinGW环境,编译windows平台Qt程序(使用MXE)
参考链接: MXE.>大多数程序都是在windows平台下开发的程序.windows 在现实中也是绕不过的一个系统平台,做为受过几年VC,MFC”虐待”的程序员,在做为一个程序员之前是一位Lin ...
- ASP.NET MVC 4下 Code First 数据库迁移
一.命令开启 1.打开控制台:视图->其他窗口->程序包管理器控制台: 2.启动数据库迁移,执行命令:enable-migrations 创建成功后会新增migrations目录等. 若 ...
- NOIP2013,复赛及同步赛,报名及比赛,专题页面
本通知的对象仅仅是福州第十九中学的学生 所有参加复赛以及同步赛的选手,请务必要仔细阅读:<关于CCF NOIP2013复赛有关事宜的通知>,里面有比赛的时间.地点.以及比赛费用的说明. 参 ...