点击打开链接

无向图点双联通。二分图判定

<span style="font-size:18px;">#include <cstdio>
#include <stack>
#include <vector>
#include <algorithm>
#include <cstring> using namespace std; struct Edge{
int u, v;
};
const int maxn = 1005;
int pre[maxn], iscut[maxn], bccno[maxn],dfs_clock, bcc_cnt;
vector<int> G[maxn], bcc[maxn]; stack<Edge> S; int dfs(int u, int fa){
int lowu = pre[u] = ++dfs_clock;
int child = 0;
for(int i=0; i<G[u].size(); ++i){
int v = G[u][i];
Edge e = (Edge){u, v};
if(!pre[v]){
S.push(e);
child++;
int lowv = dfs(v, u);
lowu = min(lowu, lowv);
if(lowv>=pre[u]){
iscut[u] = true;
bcc_cnt++; bcc[bcc_cnt].clear();
for(;;){
Edge x = S.top(); S.pop();
if(bccno[x.u]!=bcc_cnt){
bcc[bcc_cnt].push_back(x.u); bccno[x.u] = bcc_cnt;
}
if(bccno[x.v] != bcc_cnt){
bcc[bcc_cnt].push_back(x.v); bccno[x.v] = bcc_cnt;
}
if(x.u==u && x.v==v) break;
}
}
}
else if(pre[v]<pre[u] &&v!=fa){
S.push(e);
lowu = min(lowu, pre[v]);
}
}
if(fa < 0 && child == 1) iscut[u] = 0;
return lowu;
} void find_bcc(int n){
memset(pre, 0, sizeof pre );
memset(iscut, 0, sizeof iscut );
memset(bccno, 0, sizeof bccno );
dfs_clock = bcc_cnt = 0;
for(int i=0; i<n; ++i)
if(!pre[i]) dfs(i, -1);
} int odd[maxn], color[maxn];
bool bipartite(int u, int b){
for(int i=0; i<G[u].size(); ++i){
int v = G[u][i]; if(bccno[v]!=b)continue;
if(color[v]==color[u]) return false;
if(!color[v]){
color[v] = 3 - color[u];
if(!bipartite(v, b)) return false;
}
}
return true;
} int A[maxn][maxn]; int main(){
#ifndef ONLINE_JUDGE
freopen("in.cpp", "r", stdin);
freopen("out.cpp", "w", stdout);
#endif // ONLINE_JUDGE
int kase = 0, n, m;
while(scanf("%d%d", &n, &m)==2 &&n){
for(int i=0; i<n; ++i)G[i].clear();
memset(A, 0, sizeof A );
for(int i=0; i<m; ++i){
int u, v;
scanf("%d%d", &u, &v);
u--; v--;
A[u][v] = A[v][u] = 1;
}
for(int u=0; u<n; ++u)//避免重边
for(int v=u+1; v<n; ++v)
if(!A[u][v]) {
G[u].push_back(v);
G[v].push_back(u);
} find_bcc(n); memset(odd, 0, sizeof odd );
for(int i=1; i<=bcc_cnt; ++i){
memset(color, 0, sizeof color );
for(int j=0; j<bcc[i].size(); ++j) bccno[bcc[i][j]] = i;
int u = bcc[i][0];
color[u] = 1;
if(!bipartite(u, i))
for(int j=0; j<bcc[i].size(); ++j) odd[bcc[i][j]] = 1;
}
int ans = n;
for(int i=0; i<n; ++i) if(odd[i]) ans--;
printf("%d\n", ans);
}
return 0;
}
</span>

poj2942 Knights of the Round Table,无向图点双联通,二分图判定的更多相关文章

  1. poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)

    #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...

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

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

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

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

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

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

  5. 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)

    Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...

  6. UVA-1364.Knights of the Round Table 无向图BCC

    题目链接:https://vjudge.net/problem/UVA-1364 题意:有n个人参加会议,互相憎恨的人不能坐在相邻的位置,并且每个会议参加的人数必须是奇数,求有多少个人不能参加任何一个 ...

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

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

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

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

  9. POJ2942:Knights of the Round Table

    传送门 点双练习. 很简单的一道模板题,建立反图,求出点双,二分图判定奇环. //POJ 2942 //by Cydiater //2016.11.2 #include <iostream> ...

随机推荐

  1. Vmdk文件如何使用,vmdk导入虚拟机VMware8.0教程

    一.打开虚拟机VMware Workstation8.0,点击新建虚拟机. 二.进入虚拟机向导,选择自定义. 三.这里保持默认即可. 四.这里选择“我以后安装操作系统”. 五.这里选择Windows ...

  2. ios23- 文件下载(同步和异步)

    1.第一步:创建一个单例视图 #import <UIKit/UIKit.h> @interface ios23_downViewController : UIViewController& ...

  3. HDU 2098 分拆素数和(素数)

    HDU 2098 分拆素数和(素数) http://acm.hdu.edu.cn/showproblem.php?pid=2098 题意: 给你一个偶数,问你这个偶数有多少种方式能由两个不同的素数构成 ...

  4. Lucene.Net 2.3.1开发介绍 —— 二、分词(三)

    原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(三) 1.3 分词器结构 1.3.1 分词器整体结构 从1.2节的分析,终于做到了管中窥豹,现在在Lucene.Net项目中添加一个类关 ...

  5. 做web项目时对代码改动后浏览器端不生效的应对方法(持续更新)

    做web项目时,常常会遇到改动了代码,但浏览器端没有生效,原因是多种多样的,我会依据我遇到的情况逐步更新解决的方法 1.执行的时候採用debug模式,普通情况下使用项目部署button右边那个butt ...

  6. Java Web Services (0) - Overview

    前言第1章 Web服务快速入门 1.1 Web服务杂项 1.2 Web服务有什么好处 1.3 Web服务和面向服务的架构 1.4 Web服务简史 1.4.1 从DCE/RPC到XML-RPC 1.4. ...

  7. python转换时间戳和日期时间格式的转换

    [steven@txzxp2 seccenter]$ python Python 2.7.5 (default, Jul  8 2013, 09:48:59)  [GCC 4.8.1 20130603 ...

  8. oracle检查点队列与增量检查点【转载】

    oracle检查点队列与增量检查点 今天是2013-09-04,这几天一直心里安顿不下来,今天还好了,可以自己安静的学习一下oracle,在此记录一下学习笔记.这篇文章我不知道在那转载的,一直都留在我 ...

  9. hdu 5115 Dire Wolf

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目分类:区间dp 题意:有n只狼,每只狼有两种属性,一种攻击力一种附加值,我们没杀一只狼,那么 ...

  10. HUST 1569(Burnside定理+容斥+数位dp+矩阵快速幂)

    传送门:Gift 题意:由n(n<=1e9)个珍珠构成的项链,珍珠包含幸运数字(有且仅由4或7组成),取区间[L,R]内的数字,相邻的数字不能相同,且旋转得到的相同的数列为一种,为最终能构成多少 ...