点的双联通+二分图的判定(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.判断是否可以成功输出升 ...
随机推荐
- socket 中午吃的啥
http://www.cnblogs.com/thinksasa/archive/2013/02/26/2934206.html
- visual studio 2005 编fortran程序,运行后dos窗口显示问题
比如程序: program main implicit none write(*,*) "AAAAAAAAAAAAAAAAAAAAAAAA" stop end 虽然可以看见DOS窗 ...
- PureBasic 打开一个一行有多个数据的文件并读取其中某个数据
如果有一个文件如下: TITLE = "Water Wurface Elevation"VARIABLES = "X", "Y", &quo ...
- 蓝牙的Baseband说明
蓝牙的radio部分使用2.4GHz的ISM段,2400 - 2483.5 MHz,通道间隔1MHz,GFS调制,采用跳频技术,每秒至少1600次.连接完成后的跳频次数为1600次/s,在inquir ...
- java jmx
http://blog.csdn.net/qiao000_000/article/details/6063949 一.JMX简介 什么是JMX?在一篇网文中是这样说的:"JMX(Java M ...
- Solr4.3之检索建议suggest
原文链接:http://www.656463.com/article/Efm26v.htm 很多才学solr的人,都容易把solr spellcheck和solr suggest混淆,误以为他们是一样 ...
- Http响应code
Http响应报文 HTTP响应也由三个部分组成,分别是:状态行.消息报头.响应正文. 其中,HTTP-Version表示服务器HTTP协议的版本:Status-Code表示服务器发回的响应状态代码:R ...
- IE下载时提示无法下载,重试后成功
// Add Excel as content type and attachment Response.ContentType = “application/vnd.ms-excel”; Res ...
- [LeetCode]题解(python):043-Multiply Strings
题目来源 https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, retur ...
- [LeetCode]题解(python):036-Valid Sudoku
题目来源 https://leetcode.com/problems/valid-sudoku/ etermine if a Sudoku is valid, according to: Sudoku ...