传送门

点双练习。

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

//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. HTML学习(二)进阶篇

    在博客园中有许多大神对HTML超文本标记语言写了很多内容,总结了很多知识,这里对我看到的博客文章, 所学到的知识,做一个总结.  一)列表和表格 dl→definition list(定义列表),见备 ...

  2. ClassNotFoundException超限

    错误日志图 被这弱智的错误吭了半个小时,项目本来好好的,然后因为改版加了很多东西,所以就超限了,一开始总是报下面那图的错,搞的我总以为是retrofit的错,但是好好的框架而且没改过,怎么可能会出错呢 ...

  3. [译]Angular2 和TypeScript -- 一次简要的预览

    原文链接:https://www.infoq.com/articles/Angular2-TypeScript-High-Level-Overview 作者:  Yakov Fain Posted o ...

  4. Geeklink引领智慧新生活!

    煤油灯成为古董,管道天然气进入厨房,电脑挤进生活,手机代替书信成为通讯的主要工具-这些变化无不提醒我们,时代在变迁,科技在发展.而最近朋友圈和电视又在播报智能家居的生活方式-智能家电能实现怎样的情景功 ...

  5. HDOJ 2561. 第二小整数 第k大问题

    第二小整数 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  6. 【原】让H5页面适配移动设备全家 - 前端篇 - PPT

    7月份在部门内给设计中心的同事们带来<让H5页面适配移动设备全家 - 设计师篇 - PPT>的分享,在视觉和交互稿上提出页面适配的建议及提升页面体验的好处,促进前端和设计双方更好的合作,同 ...

  7. Intellij IDEA 一些不为人知的技巧

    Intellij IDEA 一些不为人知的技巧 2016/12/06 | 分类: 基础技术 | 0 条评论 | 标签: IntelliJ 分享到:38 原文出处: khotyn 今天又听了 Jetbr ...

  8. hibernate三种状态

    转自:http://www.cnblogs.com/xiaoluo501395377/p/3380270.html 学过hibernate的人都可能都知道hibernate有三种状态,transien ...

  9. 关于EventEmitter的用法

    EventEmitter的基本用法: var EventEmitter = require("events").EventEmitter; var ee = new EventEm ...

  10. [LeetCode] First Bad Version 第一个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...