POJ2942:Knights of the Round Table
点双练习。
很简单的一道模板题,建立反图,求出点双,二分图判定奇环。
//POJ 2942
//by Cydiater
//2016.11.2
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n) for(int i=j;i<=n;i++)
#define down(i,j,n) for(int i=j;i>=n;i--)
#define cmax(a,b) a=max(a,b)
#define cmin(a,b ) a=min(a,b)
#define Auto(i,node) for(int i=LINK[node];i;i=e[i].next)
#define vci vector<int>
const int MAXN=1e5+5;
const int oo=0x3f3f3f3f;
inline int read(){
char ch=getchar();int x=0,f=1;
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int LINK[MAXN],len=0,dfn[MAXN],low[MAXN],stack[MAXN],top=0,dfs_clock=0,ans,N,M,cnt=0,head,tail,q[MAXN],col[MAXN];
bool E[1005][1005],OK[MAXN],avail[MAXN];
vci block;
struct edge{
int y,next;
}e[MAXN];
namespace solution{
void Clear(){
len=dfs_clock=top=ans=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(LINK,0,sizeof(LINK));
memset(OK,0,sizeof(OK));
memset(E,0,sizeof(E));
}
inline void insert(int x,int y){e[++len].next=LINK[x];LINK[x]=len;e[len].y=y;}
inline void Insert(int x,int y){insert(x,y);insert(y,x);}
void init(){
Clear();
N=read();M=read();if(N==0)exit(0);
up(i,1,M){
int x=read(),y=read();
E[x][y]=E[y][x]=1;
}
up(i,1,N)up(j,i+1,N)if(!E[i][j])Insert(i,j);
}
bool check(){
memset(col,0,sizeof(col));
head=1;tail=0;q[++tail]=block[0];
col[block[0]]=1;
for(;head<=tail;head++){
int node=q[head];
Auto(i,node)if(avail[e[i].y]){
if(col[e[i].y]==col[node])return 1;
if(col[e[i].y]==0){
col[e[i].y]=col[node]*(-1);
q[++tail]=e[i].y;
}
}
}
return 0;
}
void color(){
int siz=block.size();
if(siz>1){
memset(avail,0,sizeof(avail));
up(i,0,siz-1)avail[block[i]]=1;
if(check())up(i,0,siz-1)OK[block[i]]=1;
}
}
void tarjan(int node){
dfn[node]=low[node]=++dfs_clock;stack[++top]=node;
Auto(i,node)if(!dfn[e[i].y]){
tarjan(e[i].y);
cmin(low[node],low[e[i].y]);
if(dfn[node]<=low[e[i].y]){
int tmp;block.clear();cnt++;
do{
tmp=stack[top--];
block.push_back(tmp);
}while(e[i].y!=tmp);
block.push_back(node);
color();
}
}else cmin(low[node],dfn[e[i].y]);
}
void slove(){
up(i,1,N)if(!dfn[i])tarjan(i);
up(i,1,N)if(!OK[i])ans++;
printf("%d\n",ans);
}
}
int main(){
//freopen("input.in","r",stdin);
using namespace solution;
while(1){
init();
slove();
}
return 0;
}
POJ2942:Knights of the Round Table的更多相关文章
- 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
问题 E: Knights of the Round Table 时间限制: 1 Sec 内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...
- POJ2942 Knights of the Round Table 点双连通分量,逆图,奇圈
题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先 ...
- POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)
题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...
- poj2942 Knights of the Round Table 双连通分支 tarjan
题解:http://blog.csdn.net/lyy289065406/article/details/6756821 讲的很详细我就不多说了. 题目连接:http://poj.org/proble ...
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...
- POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】
LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...
- POJ2942 Knights of the Round Table 点双连通分量 二分图判定
题目大意 有N个骑士,给出某些骑士之间的仇恨关系,每次开会时会选一些骑士开,骑士们会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件:1.任意相互憎恨的两个骑士不能相邻.2.开会人数为大于2的奇 ...
- [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)
建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点 ...
随机推荐
- MySQL Performance-Schema(三) 实践篇
前一篇文章我们分析了Performance-Schema中每个表的用途,以及主要字段的含义,比较侧重于理论的介绍.这篇文章我主要从DBA的角度出发,详细介绍如何通过Performance-Schema ...
- Python中对时间日期的处理方法简单汇总
这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...
- css之定位
定位有三种,分别是相对定位 position:relative; .绝对定位 position:absolute; .固定定位 position:fixed; 相对定位 相对定位,就是微调元素位置的, ...
- MySQL 临时表
MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. 临时表在MySQL 3.23版本中添加,如果你的MySQL版本 ...
- 7、二种 为二个不同的子网配置DHCP服务器(中继代理服务器)
环境如下: (参考之前,保证二个子网可以互相ping通) 虚拟机vm1 192.168.170.3 VMnet8 (NAT模式) 虚拟 ...
- JSP动作元素——————理论篇
JSP动作元素的组成及作用 JSP使用Action来控制Servlet引擎的行为,可重复使用JavaBean组件. 常用Action: jsp:param 在 jsp:include.js ...
- 解决Ubuntu下Firefox+OpenJDK没有Java插件的问题
如果是安装的OpenJDK,很遗憾它是没有libnpjp2.so的. 此时按照网上各种奇怪的方法都挣扎无效,但可以用icedtea插件来解决这个问题. icedtea的版本与本机安装的OpenJDK版 ...
- Python 【第十一章】 Django模版
1.直接传值 urls.py """mysite URL Configuration The `urlpatterns` list routes URLs to view ...
- 项目vue2.0仿外卖APP(七)
ratings评价列表页实现 在ratings.vue组件里开发 首先先引入seller数据: 书写模板结构: 由于评价页又有之前写过的star.vue组件,所以又要在ratings.vue组件引入: ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...