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 ...
随机推荐
- npm报错npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2018-03-15T01_48_14_769Z-debug.log
全局更新 npm npm i npm -g 就ok了
- 使用Kotlin开发Android应用(I):简介
Kotlin是一门基于JVM的编程语言,它正成长为Android开发中用于替代Java语言的继承者.Java是世界上使用最多的编程语言之一,当其他编程语言为更加便于开发者使用而不断进化时,Java并没 ...
- F110操作手册-自动付款
SAP 系统 F110系统操作手册 目 录 1.自动付款... 3 1.自动付款 事务代号: F110 菜单路径: 会计 →财 ...
- mysql数据库补充知识3 查询数据库记录信息之多表查询
一 介绍 准备表 company.employeecompany.department 复制代码 #建表 create table department( id int, name varchar(2 ...
- Computer Information
Lab: lxw@lxw-PC:python$ df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda7 190G .4G 175G % / none .0K .0K % /sys/ ...
- Java FAQ -- "Exception in thread 'main' java.lang.UnsupportedClassVersionError:"
OS:Ubuntu 最近重新学习Java,写了一段很小的程序,如下: public class Hello{ public static void main(String args[]){ Syste ...
- Linux 路径与命令搜寻顺序
以相对/绝对路径运行命令,例如『 /bin/ls 』或『 ./ls 』: 由 alias 找到该命令来运行: 由 bash 内建的 (builtin) 命令来运行: 透过 $PATH 这个变量的顺序搜 ...
- django-admin 修改admin自带模版
还不知道怎么指定修改每个页面,我就把把所有修改写在一个页面,通过url进行判断是否是是否显示修改内容,修改的是change_form.html ,在admin里面可以找到 {% block objec ...
- NodeJS应用程序设置为window service-辅助工具(C#)
1.修改nssm,去对话框后 2.生成批处理文件,执行 3.将nssm.exe.node.exe放在资源文件里面 附代码 工具
- Cuckoo Hash——Hash冲突的解决办法
参考文献: 1.Cuckoo Filter hash算法 2.cuckoo hash 用途: Cuckoo Hash(布谷鸟散列).问了解决哈希冲突的问题而提出,利用较少的计算换取较大的空间.占用空间 ...