题意:有一群骑士要坐在一个圆形的桌子上,他们之间有些人相互讨厌,所以不能挨着,要求算出一次也不能坐在桌子上的人,每次会议桌子必须奇数个人,一个人不能开会

题解:可以先建一个补图,要满足题目条件我们只要找出所有奇圈(奇数个点的环),求出点-双联通分量,对于每一个单独的点-双连通分量,如果它一定是一个奇圈,那么不能够通过二分图染色,可以通过画图验证这条结论,那么我们对于所有的奇圈里的点进行染色,最后输出没有染色过的点,因为有可能会出现多次染色的点,所以不能直接每次加点数

坑点:不能用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 点-双联通+二分图染色的更多相关文章

  1. 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

    [POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

  2. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  3. 点的双联通+二分图的判定(poj2942)

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10804   Acce ...

  4. POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)

    题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...

  5. poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图

    http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011 ...

  6. poj2942(双联通分量,交叉染色判二分图)

    题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 思路:首先 ...

  7. POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】

    LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...

  8. 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)

    layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...

  9. poj2942 Knights of the Round Table,无向图点双联通,二分图判定

    点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...

随机推荐

  1. mono下c#和c交互,字符串处理

    起因是ulua里,从luajit读字符串到c#里,做了编码转换,如下 public static string lua_tostring(IntPtr luaState, int index) { v ...

  2. 基于Cpython的 GIL(Global Interpreter Lock)

    一 介绍 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...

  3. python模块学习(四)

    re模块 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C ...

  4. 我的Android进阶之旅------>Android中查看应用签名信息

    一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...

  5. 2.1 使用ARDUINO控制MC20打电话

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  6. Centos----本地yum源制作

    本地YUM源制作 1.   YUM相关概念 1.1.  什么是YUM YUM(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的S ...

  7. [POI2007]立方体大作战tet

    题目 BZOJ 洛谷 做法 很巧妙的题,注意每种颜色只有两个 消除一种颜色,其实就是看中间有多少个没有被消除的块,这种动态距离问题显然能用树状数组解决 洛谷输出方案,暴力往下爬就行 My comple ...

  8. css li 间隙

    如果 li 未浮动,而 li 子元素浮动,则ie6和ie7下会出现间隙,解决办法是给 li 写上css hack      *vertical-align:bottom;

  9. 在.h和.cpp中包含头文件的区别

    1.在.h中包含头文件,是为了声明一系列这个头文件的变量等,可能会产生重复包含的问题: 2.在.cpp中包含头文件只是为了实现这个头文件或者使用其中的方法,不会有重复包含的问题,所以尽量在源文件中包含 ...

  10. Spring的AOP面向切面编程

    什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...