传送门

点双练习。

很简单的一道模板题,建立反图,求出点双,二分图判定奇环。

//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的更多相关文章

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

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

  2. 「题解」:[POJ2942]Knights of the Round Table

    问题 E: Knights of the Round Table 时间限制: 1 Sec  内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...

  3. POJ2942 Knights of the Round Table 点双连通分量,逆图,奇圈

    题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先 ...

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

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

  5. poj2942 Knights of the Round Table 双连通分支 tarjan

    题解:http://blog.csdn.net/lyy289065406/article/details/6756821 讲的很详细我就不多说了. 题目连接:http://poj.org/proble ...

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

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

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

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

  8. POJ2942 Knights of the Round Table 点双连通分量 二分图判定

    题目大意 有N个骑士,给出某些骑士之间的仇恨关系,每次开会时会选一些骑士开,骑士们会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件:1.任意相互憎恨的两个骑士不能相邻.2.开会人数为大于2的奇 ...

  9. [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)

    建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点                             ...

随机推荐

  1. co源码解读

    背景: 闲来无事,翻了下co的源码来看,源码短小精悍,算上注释,一共240行左右: 决定写一篇博客来记录下学习的心得. TJ大神的co:https://github.com/tj/co 作用: co通 ...

  2. ABAP关键字SUBMIT的简单例子和学习小记

    网上有关SUBMIT实现程序调用的例子稍显复杂,而相关的参考和解释则不是很完善.本文给出一个SUBMIT的小示例程序(代码见文末),实现了最简单的程序间调用及返回值,以及SAP官方文档中相关内容的翻译 ...

  3. 调用sharepoint 2010 REST报版本过低

    问题描述: 写了一个webservice调用sharepoint REST,本机测试成功,部署到服务器上后报错 (System.Data.Services.Client.DataServiceQuer ...

  4. django 第三天 有关pip使用

    软件应用开发的经典模型有这样几个环境:开发环境(development).集成环境(integration).测试环境(testing).QA验证,模拟环境(staging).生产环境(product ...

  5. Java使用POS打印机(无驱)

    使用原因:应项目要求,需要使用打印机,但是如果使用Windows驱动来实现打印,在某些条件下会发生网络堵塞等,而且没有提示,所以为了确保信息的完整,避免数据丢失.我们使用无驱打印(直接写端口的方法), ...

  6. Yii2.0.7 限制user module登录遇到的问题

    在Yii2.0.6的时候我是在以下文件通过以下方法实现的. frontend/modules/user/Module.php namespace frontend\modules\user; clas ...

  7. 基于jquery实现图片拖动和曲线拖放

    功能:图片的拖动.曲线的拖放和绘制 一. 准备工作 1. 点击此下载相关的文档 二. 在浏览器中运行 dragDrop.html 文件,即可看到效果 三. 效果图

  8. 前端少侠的ps故事

    前端少侠的ps故事 正所谓,码在江湖,身不由己.自21世纪前后端分离,代码分工细化以来,前端与设计的合作也变得越来越重要.有人说,如果前端懂设计的话,工作会更快一点.倘若说我入前端半年能算半个前端少侠 ...

  9. Caffe Python MemoryDataLayer Segmentation Fault

    转载请注明出处,楼燚(yì)航的blog,http://home.cnblogs.com/louyihang-loves-baiyan/ 因为利用Pyhon来做数据的预处理比较方便,因此在data_l ...

  10. MongoDB查询转对象是出错Element '_id' does not match any field or property of class

    MongoDB查询转对象是出错Element '_id' does not match any field or property of class   解决方法: 1.在实体类加:[BsonIgno ...