题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18122

【思路】

点-双连通分量

求出bcc,判断每个bcc是否为二分图,如果不是二分图则bcc中一定存在一个奇圈,则bcc中的任意一点一定位于一个奇圈上。

【代码】

 #include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
using namespace std; typedef long long LL;
const int maxn = +; struct Edge{ int u,v;
}; 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 ch=;
for(int i=;i<G[u].size();i++) {
int v=G[u][i];
Edge e=(Edge) {u,v};
if(!pre[v]) {
S.push(e);
ch++;
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u]) {
iscut[u]=;
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< && ch==) 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 color[maxn],odd[maxn];
bool judge(int u,int b) {
for(int i=;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]=-color[u];
if(!judge(v,b)) return false;
}
}
return true;
} int n,m;
int A[maxn][maxn]; void init() {
memset(A,,sizeof(A));
for(int i=;i<n;i++) G[i].clear();
} int main() {
while(scanf("%d%d",&n,&m)== && n ) {
init();
int u,v;
for(int i=;i<m;i++) {
scanf("%d%d",&u,&v);
u--,v--;
A[u][v]=A[v][u]=;
}
for(int i=;i<n;i++) for(int j=i+;j<n;j++)
if(!A[i][j]) G[i].push_back(j),G[j].push_back(i);
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(!judge(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 ;
}

UVAlive3523 Knights of the Round Table(bcc)的更多相关文章

  1. UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)

    题目大意:有n个骑士要在圆桌上开会,但是相互憎恶的两个骑士不能相邻,现在已知骑士们之间的憎恶关系,问有几个骑士一定不能参加会议.参会骑士至少有3个且有奇数个. 题目分析:在可以相邻的骑士之间连一条无向 ...

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

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

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

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

  4. 【POJ】2942 Knights of the Round Table(双连通分量)

    http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...

  5. POJ 2942 Knights of the Round Table (点双连通分量)

    题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...

  6. UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)

    尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...

  7. UVALive 3523 : Knights of the Round Table (二分图+BCC)

    题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; ; int n,m; int dfn[maxn],low[ ...

  8. 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)

    圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...

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

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

随机推荐

  1. discuz! X3 门户文章添加字段

    1. 首先需要去数据表里[llgp_portal_article_title]手动添加需要添加的字段. (注意: 数据表前缀依据自己的设置而定) 2. 修改模版template\default\por ...

  2. CSS before和after伪元素

    CSS中有一个特性允许我们添加额外元素而不扰乱文档本身,它们是以CSS选择器的形式出现的,具有标签的表现效果,但是呢又不是真正的标签元素,所以叫做“伪元素”.下面就说一下常见的两个伪元素before和 ...

  3. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  4. 自己总结python用xlrd\xlwt读写excel

    1.首先安装xlrd\xlwt模块 xlrd模块下载地址: https://pypi.python.org/pypi/xlrd xlwt模块下载地址: https://pypi.python.org/ ...

  5. 获取当前元素节点的position和宽高(兼容)

    function objxy(n){ var o=document.getElementById(n),x=0,y=0,w=o.offsetWidth,h=o.offsetHeight if(o.ge ...

  6. 002.AngularJs调用Restful实现CRUD

    本节我们主要给大家介绍AngularJs如何调用Restful,实现数据的CRUD. 主要用到的技术: 后端:ASP.NET WebApi + SQLServer2008 前端:AngularJs,B ...

  7. yii 标签用法(模板)

    yii模板中的label标签 <?php echo $form->labelEx($model,'name'); ?> 编译后: <label for="Projec ...

  8. CentOS 5.6服务器配置YUM安装Apache+php+Mysql+phpmyadmin

    1. 更新系统内核到最新. [root@linuxfei ~]#yum -y update 系统更新后,如果yum安装时提示错误信息,请执行以下命令修复. [root@linuxfei ~]#rpm ...

  9. python -- 函数传参

    一.参数传入规则 可变参数允许传入0个或任意个参数,在函数调用时自动组装成一个tuple: 关键字参数允许传入0个或任意个参数,在函数调用时自动组装成一个dict: 1. 传入可变参数: def ca ...

  10. python之sys模块

    38.python的sys模块: 用于提供对Python解释器相关的操作: 1 2 3 4 5 6 7 8 9 sys.argv           命令行参数List,第一个元素是程序本身路径 sy ...