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(点双+二分图判定——染色法)
建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点 ...
随机推荐
- 实用的圆形图片控件ImageView
1.用法直接在布局中引用即可 import android.content.Context;import android.content.res.TypedArray;import android.g ...
- Oracle SGA优化
oracle的SGA:数据库的系统全局区,SGA主要由三部分构成:共享池.数据缓冲区.日志缓冲区. 共享池又由两部分构成:共享SQL区和数据字典缓冲区. 共享SQL区专门存放用户SQL命令,oracl ...
- 机器学习实战笔记(Python实现)-06-AdaBoost
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- [jquery]将当前时间转换成yyyymmdd格式
如题: function nowtime(){//将当前时间转换成yyyymmdd格式 var mydate = new Date(); var str = "" + mydate ...
- Servlet编程
Servlet编程 1. servlet概念及相关接口简介 java Servlet是运行在web服务器或应用服务器上的程序,他是作为来自web浏览器或其他HTTP客户端的请求和HTTP服务器山的数据 ...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...
- 安全测试 - 端口嗅探工具Nmap
Nmap 在官网下载nmap端口检测工具https://nmap.org/,nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端. 使用: 通过cmd命令:nmap www.5i5j.c ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- FineUI(专业版)公测版发布(这速度,真TM快!)
经过近一年的筹备.编码和测试,FineUI(专业版)公测版终于和大家见面了!现在就来体验一下专业版飞一般的速度吧:http://fineui.com/demo_pro/FineUI(专业版)首页:ht ...
- 强大的支持多文件上传的jQuery文件上传插件Uploadify
支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...