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 ...
随机推荐
- AngularCli项目中添加字体图标(Font)详解
本文主要讲如何在AngularCli生成的项目中使用字体图标. 一 SVG图标准备 将需要转换为字体图标的图片转换为SVG格式. 这个让项目视觉设计师搞定即可. 二 SVG图标转Font 可以通过Ic ...
- setlocale同mbstowcs函数的关系(VS2008下setlocale(LC_ALL, "chs")可以执行成功,BCB使用setlocale(LC_ALL, "Chinese (Simplified)_People's Republic of China"),linux上locale别名表大概在 /usr/lib/X11/locale/locale.alias)
序中,如果要将ASCII码字符串转换为宽字符(Unicode),可以利用标准C的mbstowcs函数. 微软在MSDN中有示例,如下: 然而,这段代码在处理含有汉字的字符串时就会出现问题.比如将: w ...
- 自定义gradle plugin
最近开始接触gradle 正好有个需求apidoc
- user_admin
# -*- coding:utf-8 -*- from django.contrib import admin from django.contrib.auth.models import User ...
- 2015.7.14(大盘结束红色,中色连坐4T)
中色今天的盘面相当有意思,现场直播庄家和散户斗法我估计中色要拉涨停了,不过你别跟,现在很危险了——就算是涨停,明天一个低开就把你给绕进去了 1.今天开市9:42发现中色的地位买入点良机16.13,此时 ...
- pyhton3 random模块
random是内建(built-in)函数,作用是产生随机数 导入模块: import random 接着就可以调用random模块下的函数了使用 dir(random)可以查看random模块下有哪 ...
- Kattis - pseudoprime 【快速幂】
题意 给出两个数字 P 和 A 当p 不是素数 并且 满足a^p≡a(mod p) 就输出 yes 否则 输出 no 思路 因为 数据范围较大,用快速幂 AC代码 #include <cstdi ...
- DEC VT100 terminal
- Java数据类型 及 转换原则
一.数据类型分类:主要分为 基本类型.引用类型两大类: 二.基本类型 转换原则 1.类型转换主要在在 赋值.方法调用.算术运算 三种情况下发生. a.赋值和方法调用 转换规则:从低位类型到高位类型自动 ...
- imx6qsbd lvds dtc
lvds显示屏调试参考 1.基于飞思卡尔imxsolosabresd开发板Linux-3.10.53 lvds屏幕调试: http://blog.csdn.net/qq_37375427/articl ...