点的双联通+二分图的判定(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.判断是否可以成功输出升 ...
随机推荐
- 【转】unity地形插件T4M使用帮助
unity的地形系统在手机游戏中因为效率问题基本无法使用,只能通过T4M这个地形插件来进行优化制作.下面大概讲解一下使用流程及方法. 先中U3D里面用自带的地形系统刷出想要的地形和贴图.贴图可以大概刷 ...
- Instance of 和getClass()区别概述
instance of 运算符和getClass()方法都可以用来检查一个对象所属的类.instance of 运算符仅测试一个对象的类型:getClass()方法与==或!=结合使用,测试两个对象是 ...
- Redis学习笔记(2)-String
package cn.com; import java.util.List; import redis.clients.jedis.Jedis; public class Redis_String { ...
- 【转】用树莓派搭建web服务器
本文将详细介绍如何在树莓派上配置服务器,和<教你在Xubuntu上搭建LAMP服务器>有些类似,多了一些介绍在树莓派上的不同步骤的地方. 这种服务器的配置被称为LAMP,是最流行的服务器配 ...
- Linus:利用二级指针删除单向链表
Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...
- hive中关于数据库与表等的基本操作
一:基本用法 1.新建数据库 2.删除数据库 3.删除非空的数据库 4.指定数据库的位置 LOCATION:指定数据库的位置,不会在系统的默认文件下. 5.在指定数据库中新建表(验证在指定的数据库中可 ...
- Android笔记:利用InputStream和BufferedReader 进行字节流 字符流处理
通过socket获取字节流处理时最初使用的是BufferedReader和PrintWriter 这种方式在解析字符串时是比较方便的 但是在处理字节时不够方便最终还是回归到InputStream和O ...
- 对于限制UITextView输入的字符数
对于限制UITextView输入的字符数.相信大家在网上见得最多的是实现UITextViewDelegate 摘自:方法 - (void)viewDidLoad { self.titleText = ...
- Appium-001-测试开发环境搭建(Android - Win7)
随着移动端 App 测试自动化的兴起,为更好的控制产品质量,越来越多的中大型公司开始了移动端的自动化测试.Appium 自动化测试技术也是我很早之前就想学习的一门技术,却一直没有比较空余的时间来学习( ...
- 安装Win7或者XP系统用虚拟光驱加载Win7或者XP镜像 iso文件xp win7wim文件
安装Win7或者XP系统用虚拟光驱加载Win7或者XP镜像 iso文件xp win7wim文件 http://pcedu.pconline.com.cn/teach/xt/1201/2657834_8 ...