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> ...
随机推荐
- System Request 进入KDB模式过程详解
0 echo g > /proc/sysrq-trigger 怎么让系统停下来,进入进入KDB循环? 1 需要简单了解下:Linux Magic System Request 2 ...
- hadoop出现ava.lang.ClassNotFoundException: org.codehaus.jackson.map.JsonMappingException
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/map/JsonMa ...
- AngularJs 父子级Controller传递数据
<div ng-controller="MyAccountCtrl"> <div ng-controller="TransferCtrl"&g ...
- spring mvc 提交数组等复杂类型
使用jquery提交,比如monthIncome的值是一个数组,在Java里用request.getParameterValues("monthIncome");取不到值,要这样才 ...
- PowerShell与Unix Shell对比:八大实例
PowerShell与Unix Shell对比:八大实例 本文将从八个实例对比PowerShell和Unix Shell,通常是Linux Bourne Shell(包括sh.ksh和bash等).二 ...
- 编写在浏览器中不弹出警告的ActiveX控件
我们在编写ActiveX控件时,如果用在浏览器中,经常都会弹出现在运行的脚本不安全的提示, 如果给客户使用,将会带来极大不便.按照MSDN的介绍通常有两种一种是实现IObjectSafe接口,一种是通 ...
- HDU 3549 Flow Problem(有向边网络流)
九野的博客,转载请注明出处 :http://blog.csdn.net/acmmmm/article/details/11221561 题意:T个测试数据 下面n,m表示n个点m条有向带权边 m条边 ...
- Spring整合的quartz任务调度的实现方式
一.在web.xml中将配置文件的位置指定好. Web.xml的配置如下: <?xmlversion="1.0"encoding="UTF-8"?> ...
- 关于window.history.back()后退问题
Windows下的window.history.back()后退后返回的不仅仅是前一个页而是前一个页的状态.假设一个页我改动了3次那必须后退3次才干回到前一个页.并且数据库中删除的数据依旧显示在上面感 ...
- groovy : 正則表達式
groovy 正則表達式 企图模仿Perl 的语法,结果是我试用后.发现没法提取匹配的字符串. 还是直接引用 java.util.regex 负责对字符序列进行正則表達式匹配 先转载水木清华上的样例 ...