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> ...
随机推荐
- Win7+vs2010下安装boost_1_46_1库
一.boost库分类: (1)不需要编译库:any.array.asio.conversion.crc.bind/mem_fn.enable_if.function.lambda.mpl.smart_ ...
- Java中取某一个范围的随机数
一.取模操作 public static void main(String[] args) { for (int i = 1; i <= 20; i++) { int j = i % 11; S ...
- 积累的VC编程小技巧之编辑框
1.如何让对话框中的编辑框接收对话框的消息 ////////////////////////////////////////////////// 如何让对话框中的CEdit控件类接收对话框的消息/// ...
- (原创)(C#随笔)IEnumerable< ICollection < IList区别
public interface IEnumerable { IEnumerator GetEnumerator(); } 再看ICollection<T> public interfac ...
- one command 一键收集 oracle 巡检信息(包括dbhc,awr reports)
初步效果图例如以下 SQL> @nb ------Oracle Database health Check STRAT ------Starting Collect Data Informati ...
- [Java][Android][Process] ProcessBuilder与Runtime差别
在Android中想要进行Ping,在不Root机器的情况下似乎还仅仅能进行底层命调用才干实现. 由于在Java中要进行ICMP包发送须要Root权限. 于是仅仅能通过创建进程来攻克了.创建进程在Ja ...
- ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六)
原文:ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六) ThinkPHP多应用配置技巧(没有使用分组,这是通过入口文件产生的Home.Admin)----很实用! 比如:现在有Ho ...
- PHP网站安装程序的原理及代码
原文:PHP网站安装程序的原理及代码 原理: 其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安 ...
- [Android阅读代码]android-async-http源码学习一
android-async-http 下载地址 一个比较常用的Http请求库,基于org.apache.http对http操作进行封装. 特点: 1.每一个HTTP请求发生在UI线程之外,Client ...
- JVM内存配置详解(转)
前段时间在一个项目的性能测试中又发生了一次OOM(Out of swap sapce),情形和以前网店版的那次差不多,比上次更奇怪的是,此次搞了几天之后啥都没调整系统就自动好了,死活没法再重现之前的O ...