具体题解看大白书P316

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <stack>
#include <cstdio>
using namespace std;
struct Edge{int u,v;};
const int maxn = +;
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=;
for(int i=; i<G[u].size(); i++){
int v=G[u][i];
Edge e = (Edge){u,v};
if(!pre[v]){ // v没有访问过
S.push(e);
child++;
int lowv=dfs(v,u);
lowu = min(lowu,lowv);// 用后代的 low函数更新自己
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<&&child==) iscut[u]=;
return lowu;
}
void find_bcc(int n){
memset(pre,,sizeof(pre));
memset(iscut,,sizeof(iscut));
memset(bccno,,sizeof(bccno));
dfs_clock=bcc_cnt=;
for(int i= ; i<n ; i++)
if(!pre[i]) dfs(i,-);
}
int odd[maxn],color[maxn];
bool bipartite(int u, int b){
for(int i=; i<G[u].size(); ++i){
int v=G[u][i];
if(bccno[v]!=b) continue;
if(color[u] == color[v]) return false;
if(!color[v]){
color[v]= - color[u];
if(!bipartite(v,b)) return false;
}
}
return true;
}
int A[maxn][maxn];
int main()
{
int kase=, n,m;
while(scanf("%d%d",&n,&m)==&&n){
for(int i=; i<n; i++) G[i].clear();
memset(A,,sizeof(A));
for(int i=; i<m; ++i){
int u,v;
scanf("%d%d",&u,&v);
u--; v--;
A[u][v]=A[v][u]=;
}
for(int u=; u<n; ++u)
for(int v=u+; v<n; ++v)
if(!A[u][v]){
G[u].push_back(v); G[v].push_back(u);
}
find_bcc(n);
memset(odd,,sizeof(odd));
for(int i=; i<=bcc_cnt; ++i){
memset(color,,sizeof(color));
for(int j=; j<bcc[i].size(); ++j)
bccno[bcc[i][j]]=i;
int u = bcc[i][];
color[u]=;
if(!bipartite(u,i))
for(int j=; j<bcc[i].size(); ++j)
odd[bcc[i][j]]=;
}
int ans=n;
for(int i=; i<n; ++i)
if(odd[i]) ans--;
printf("%d\n",ans);
}
return ;
}

la3523 白书例题 圆桌骑士 双联通分量+二分图的更多相关文章

  1. 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)

    layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...

  2. Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定

    题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...

  3. POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug

    题面还好,就不描述了 重点说题解: 由于仇恨关系不好处理,所以可以搞补图存不仇恨关系, 如果一个桌子上面的人能坐到一起,显然他们满足能构成一个环 所以跑点双联通分量 求点双联通分量我用的是向栈中pus ...

  4. POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)

    题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...

  5. 大白书中无向图的点双联通分量(BCC)模板的分析与理解

    对于一个无向图,如果任意两点至少存在两条点不重复(除起点和终点外无公共点)的路径,则这个图就是点双联通. 这个要求等价于任意两条边都存在于一个简单环(即同一个点不能在圈中出现两次)中,即内部无割点. ...

  6. poj2942(双联通分量,交叉染色判二分图)

    题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 思路:首先 ...

  7. 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

    [POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

  8. 【UVA10972】RevolC FaeLoN (求边双联通分量)

    题意: 给你一个无向图,要求把所有无向边改成有向边,并且添加最少的有向边,使得新的有向图强联通. 分析: 这题的解法还是很好想的.先用边双联通分量缩点,然后找新图中入度为0和为1的点,入度为0则ans ...

  9. lightoj 1300 边双联通分量+交叉染色求奇圈

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...

随机推荐

  1. python2.0_s12_day9_协程&多线程和cpu,磁盘io之间的关系

    事件驱动和异步io有什么直接关系. 当我们访问一个网页,不考虑网络问题.我们人类不觉得网页慢. 但是实际中对计算机来说还是慢.那慢在哪里.io io操作是整个网络操作中最慢的.比如你打开网页要是有2秒 ...

  2. unity动态加载(翻译) .

    AssetBundles are files which you can export from Unity to contain assets of your choice. These files ...

  3. 使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h"

    使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h",解决方法如下: 1>.在xcode中左边选中项目的root节点,在中间编 ...

  4. iPad UIPopoverController弹出窗口的位置和坐标

    本文转载至:http://blog.csdn.net/chang6520/article/details/7921181 TodoViewController *contentViewControll ...

  5. Android 使用DatePicker以及TimePicker显示当前日期和时间

    课程内容1.介绍DatePicker和TimePicker两种实现动态输入日期和事件的功能2.介绍DatePickerDialog和TimePickerDialog来年耕种实现动态输入日期和事件的对话 ...

  6. go练习2-go的学习资料

    好吧 我承认,有自己添加的内容也有从别人的blog 中 ctrl + c 的 官方:http://golang.org ,经常被封 中文手册的翻译:http://code.google.com/p/g ...

  7. linux shell中FS、OFS、RS、ORS图解

    在linux 中,总是会忘记FS\OFS\RS\ORS的使用 下面一张图非常明晰的显示

  8. Python老王视频习题答案

    基础篇2:一切变量都是数据对象的引用sys.getrefcount('test') 查看引用计数变量命名不能以数字开头编码:ascii.unicode.utf-81.阅读str对象的help文档,并解 ...

  9. Mac OS X运行程序出现bad interpreter: operation not permitted的解决方案

    最近想在我的mac笔记本上安装gvim,从官网上下载了程序后竟然非常诡异的双击无法打开,命令行执行时系统报错: /bin/sh bad interpreter operation not permit ...

  10. SpringBoot SpringApplication底层源码分析与自动装配

    目录 抛出问题 @SpringBootApplication注解剖析 SpringApplication类剖析 第一步:配置SpringBoot Bean来源 第二步 :自动推断SpringBoot的 ...