poj2942 Knights of the Round Table,无向图点双联通,二分图判定
无向图点双联通。二分图判定
<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,无向图点双联通,二分图判定的更多相关文章
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...
- POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)
题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...
- 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 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- UVA-1364.Knights of the Round Table 无向图BCC
题目链接:https://vjudge.net/problem/UVA-1364 题意:有n个人参加会议,互相憎恨的人不能坐在相邻的位置,并且每个会议参加的人数必须是奇数,求有多少个人不能参加任何一个 ...
- POJ2942 Knights of the Round Table 点双连通分量,逆图,奇圈
题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先 ...
- [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)
建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点 ...
- POJ2942:Knights of the Round Table
传送门 点双练习. 很简单的一道模板题,建立反图,求出点双,二分图判定奇环. //POJ 2942 //by Cydiater //2016.11.2 #include <iostream> ...
随机推荐
- 【Demo 0006】Java基础-类多态性
本章学习要点: 1. 了解Java多态特性; 2. 掌握Java多态的实现: 一.多态特性 1. 定义: 指同一个对象调用相同的方法实现 ...
- mmtests使用简介
1.简介 mmtests是一个可配置的测试套件,可以被MM开发者用来进行一个常规测试.理想情况下,它可以与LTP,xfstests等测试工具结合起来实现自动化测试. 2.软件组织 run-mmtest ...
- ExtJs4 笔记(11) Ext.ListView、Ext.view.View 数据视图
本篇介绍两个用来展示数据的容器控件,分别是Ext.ListView和Ext.view.View.Ext.ListView就是大名鼎鼎的Ext GridPanel的前身,不过现在的Ext4已经将它整合到 ...
- Firemonkey绑定对象列表
在实现Firemonkey绑定对象列表的过程中,我遇到的一些现有教程当中没有提到的细节,分享一下. 1.追加对象 用Navigator插入记录,位置总是在当前记录之前插入,没有在最后追加一个对象的方法 ...
- ubuntu10.10 tftp安装,配置,测试
ubuntu10.10 tftp安装,配置,测试 成于坚持,败于止步 虽然ubuntu/centos/redhat都是linux,但是内核其中存在一定的修改,所以对于tftp服务器的安装存在不同的命令 ...
- SQL查询数据封装JavaBean对象
public static List getListBySql(String sql, Class cls){ List list = new ArrayList(); Connection ...
- ExtJs4 笔记(5) Ext.Button 按钮
id="li2"></li> <li id="li3"></li> </ul> </div> ...
- Cocos2d-x layout (二)
相对某个控件进行布局 Size widgetSize = Director::getInstance()->getWinSize(); Text* alert = Text::create(&q ...
- 呜呼!Node.js是什么?
近期看到非常多站点都使用node.js.開始感到非常好奇.就開始推測这是个什么东西,大概就是个js文件吧,所以開始根本 没有在意,可是越感觉就认为越不正确劲,为什么大家都在用它呢?所以我决定搞个明确. ...
- TForm.ShowModal只是接管消息循环,禁止外部键盘和鼠标输入到别的窗口,但并不封锁其它窗口继续获取消息(比如WM_TIMER消息仍可被发送到别的窗口上)
窗体上放一个TTimer,然后双击输入: procedure TForm1.Timer1Timer(Sender: TObject); var cvs: TCanvas; Rect: TRect; S ...