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(点双+二分图判定——染色法)
建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点 ...
随机推荐
- 我的屌丝giser成长记-工作篇之A公司
A公司是我研究生毕业的第一家GIS公司,一家专门做GIS应急的公司,接的项目还是可以的.A公司的项目框架GIS部分采取的是flexviwer,当然最近一两年来,flex技术在gis行业慢慢的被淘汰了, ...
- 用 Excel 测试“绘制两点间连线”的算法
最近在研究和制作数字示波器,其中涉及一个小算法:需要将 ADC 采样的数值在 TFT LCD 屏幕上面显示并且用“线”连接起来. ADC 按照时序对输入电压采样后,记录的是一个个的数值,如果显示的时候 ...
- 离开Autodesk,开启新篇章
我已经离开了Autodesk,开启新篇章.在过去7年多时间中,我先后支持Autodesk 基础设施相关产品的开发,包括MapGuide/AIMS,Map3D,Civil 3D,Infraworks等, ...
- 简单的学习心得:网易云课堂Android开发第六章SQLite与ContentProvider
一.SQLite 1.基本操作: (1)创建数据库:在SQLiteOpenHelper的子类构造器中创建. (2)创建表:在SQLiteOpenHelper的子类onCreate方法中,调用execS ...
- Android中使用GridView和ImageViewSwitcher实现电子相册简单功能
我们在手机上查看相册时,首先看到的是网格状的图片展示界面,然后我们选择想要欣赏的照片点击进入,这样就可以全屏观看该照片,并且可以通过左右滑动来切换照片.如下图的显示效果: 首先我们先罗列一下本次实现所 ...
- 获取当前应用的系统路径工具类和java的System.getProperty()方法介绍
java的System.getProperty()方法可以获取的值,如下: 对于Java程序,无论是未打包的还是打包的JAR或WAR文件,有时候都需要获取它运行所在目录信息,如何做到这一点呢? /** ...
- 转:IE兼容模式下 SCRIPT1028: 缺少标识符、字符串或数字
IE兼容模式下 SCRIPT1028: 缺少标识符.字符串或数字例如下面一段代码 var a = { x: 1, y: 2,};alert(a.x);如果在IE的兼容性视图(IE7文档模式 ...
- java获取日期之间天数的方法
//获取两个日期之间的天数private int daysBetween(Date now, Date returnDate) { Calendar cNow = Calendar.getInstan ...
- 文本处理三剑客之sed命令
第十八章.文本处理三剑客之sed命令 目录 sed介绍 sed命令常用选项 sed常用编辑命令 sed使用示例 sed高级语法 18.1.sed简介 sed全名stream editor,流编辑器,s ...
- java中的浮点数
浮点数值不适用于禁止出现舍入误差的金融计算中.例如,命令System.out.println(2.0-1.1)将打印出0.8999999999999999999999999,而不是人们想象的0.9.其 ...