poj2942 点-双联通+二分图染色
题意:有一群骑士要坐在一个圆形的桌子上,他们之间有些人相互讨厌,所以不能挨着,要求算出一次也不能坐在桌子上的人,每次会议桌子必须奇数个人,一个人不能开会
题解:可以先建一个补图,要满足题目条件我们只要找出所有奇圈(奇数个点的环),求出点-双联通分量,对于每一个单独的点-双连通分量,如果它一定是一个奇圈,那么不能够通过二分图染色,可以通过画图验证这条结论,那么我们对于所有的奇圈里的点进行染色,最后输出没有染色过的点,因为有可能会出现多次染色的点,所以不能直接每次加点数
坑点:不能用stl,tle了好多发,最后把所有的vector,map都换成了数组就过了,不能用vector存图,那么就用我最喜欢的链式前向星吧= =
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std;
using namespace __gnu_cxx; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct edge{
int to,Next;
}e[maxn];
int bcc[N];
int index,num;
int cnt,head[N];
int dfn[N],low[N];
int bccno[N];
struct ewedge{int from,to;};
stack<ewedge>s;
bool ma[N][N];
int in[N],ok[N];
int color[N];
bool notsub;
void add(int x,int y)
{
e[cnt].to=y;
e[cnt].Next=head[x];
head[x]=cnt++;
e[cnt].to=x;
e[cnt].Next=head[y];
head[y]=cnt++;
}
void dfs(int u,int f,int p)
{
if(notsub)return ;
color[u]=p;
int c=-p;
for(int i=head[u];~i;i=e[i].Next)
{
int x=e[i].to;
if(ok[x])
{
if(!color[x])dfs(x,u,c);
else
{
if(color[x]!=c)notsub=;
}
}
}
}
void tarjan(int u,int f)
{
low[u]=dfn[u]=++index;
for(int i=head[u];~i;i=e[i].Next)
{
int x=e[i].to;
ewedge e=(ewedge){u,x};
if(x==f)continue;
if(!dfn[x])
{
s.push(e);
tarjan(x,u);
low[u]=min(low[u],low[x]);
if(low[x]>=dfn[u])
{
int res=;
num++;
memset(ok,,sizeof ok);
int be=;
for(;;)
{
ewedge p=s.top();s.pop();
if(bccno[p.from]!=num)
{
bcc[res++]=p.from;
be=p.from;
bccno[p.from]=num;
ok[p.from]=;
}
if(bccno[p.to]!=num)
{
bcc[res++]=p.to;
be=p.to;
bccno[p.to]=num;
ok[p.to]=;
}
if(p.from==e.from&&p.to==e.to)break;
}
//判断是不是二分图
for(int i=;i<res;i++)
color[bcc[i]]=;
notsub=;
dfs(be,-,);
if(notsub)
{
for(int i=;i<res;i++)
in[bcc[i]]=;
}
/* cout<<notsub<<"--------";
for(int j=0;j<bcc.size();j++)
cout<<bcc[j]<<" ";
cout<<endl;*/
}
}
else
{
if(dfn[x]<dfn[u])low[u]=min(low[u],dfn[x]);
}
}
}
void init(int n)
{
memset(head,-,sizeof head);
memset(ma,,sizeof ma);
for(int i=;i<=n;i++)
{
bccno[i]=dfn[i]=low[i]=in[i]=;
}
while(!s.empty())s.pop();
index=num=cnt=;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m)break;
init(n);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
ma[a][b]=ma[b][a]=;
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(!ma[i][j])
add(i,j);
for(int i=;i<=n;i++)
if(!dfn[i])
tarjan(i,-);
int ans=;
for(int i=;i<=n;i++)
if(in[i])
ans++;
printf("%d\n",n-ans);
}
return ;
}
/************ ************/
poj2942 点-双联通+二分图染色的更多相关文章
- 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- 点的双联通+二分图的判定(poj2942)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10804 Acce ...
- POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)
题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...
- poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图
http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011 ...
- poj2942(双联通分量,交叉染色判二分图)
题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 思路:首先 ...
- POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】
LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...
- 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)
layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...
随机推荐
- Qt Creator 如何支持并行?
PRO 文件中加入以下语句即可 #OpenMP QMAKE_CXXFLAGS += -openmp QMAKE_LFLAGS += -openmp
- Python 获取文件路径及文件目录
import os print (os.path.dirname(__file__)) print (os.path.abspath(__file__)) print (os.path.abspath ...
- wget: unable to resolve host address “http”
[root@one ~]# wget www.baidu.com --2017-09-24 10:20:23-- http://www.baidu.com/ Resolving http... fai ...
- 001-maven下载jar后缀为lastUpdated问题
问题简述 Maven在下载仓库中找不到相应资源时,网络中断等,会生成一个.lastUpdated为后缀的文件.如果这个文件存在,那么即使换一个有资源的仓库后,Maven依然不会去下载新资源. 解决方案 ...
- 顽石系列:CSS实现垂直居中的五种方法
顽石系列:CSS实现垂直居中的五种方法 在开发过程中,我们可能沿用或者试探性地去使用某种方法实现元素居中,但是对各种居中方法的以及使用场景很不清晰.参考的内容链接大概如下: 行内元素:https:// ...
- PAT 天梯赛 L1-037. A除以B 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-037 AC代码 #include <iostream> #include <cstdio&g ...
- 蓝牙固件升级(OTA升级)原理设计
转:http://blog.csdn.net/yueqian_scut/article/details/50849033 固件空中升级(OTA)与固件二次引导的原理和设计 原创 2016年03月10日 ...
- php数组函数-array_diff()
array_diff()函数返回两个数组的差集数组.该数组包括了所有在被比较数组中,但是不在任何其他参数数组中的键值在返回数组中,键名保持不变. array_diff(array1,array2,ar ...
- Deep Learning概述
1.深度学习发展简史 2.三步实现深度学习 2.1Neural Network 神经网络由模仿脑部神经系统发展而来,一个节点称为一个“Neuron”,包括连接在节点上面的weights和biases. ...
- Go Mysql驱动
Golang中MYSQL驱动 Mysql库https://github.com/go-sql-driver/mysql Go本身不提供具体数据库驱动,只提供驱动接口和管理. 各个数据库驱动需要第三方实 ...