UVAlive3523 Knights of the Round Table(bcc)
题目链接: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)的更多相关文章
- UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)
题目大意:有n个骑士要在圆桌上开会,但是相互憎恶的两个骑士不能相邻,现在已知骑士们之间的憎恶关系,问有几个骑士一定不能参加会议.参会骑士至少有3个且有奇数个. 题目分析:在可以相邻的骑士之间连一条无向 ...
- 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- 【POJ】2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...
- POJ 2942 Knights of the Round Table (点双连通分量)
题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...
- UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)
尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...
- UVALive 3523 : Knights of the Round Table (二分图+BCC)
题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; ; int n,m; int dfn[maxn],low[ ...
- 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)
圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...
- POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)
题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...
随机推荐
- TabBarController创建及使用方法简介
TabBarController创建及使用方法简介 大致讲解一下TabBarController的创建过程: 首先,我们需要一些视图,如创建UIControllerView类型的view1,view2 ...
- 谈谈iOS中粘性动画以及果冻效果的实现
在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: https://github.c ...
- Android实现网络多线程断点续传下载(转)
本示例介绍在Android平台下通过HTTP协议实现断点续传下载. 我们编写的是Andorid的HTTP协议多线程断点下载应用程序.直接使用单线程下载HTTP文件对我们来说是一件非常简单的事.那么,多 ...
- POJ 1265 Area POJ 2954 Triangle Pick定理
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5227 Accepted: 2342 Description ...
- 超级钢琴 2010年NOI
/* 自己yy的奇葩做法居然A了23333 不过空间好像很大 时间好像略慢..... 毕竟不是正解 前缀维护sum值 枚举区间起点 然后终点的坐标可以确定在一个范围 可持久化线段树查询区间第1大 然后 ...
- (转)PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
在看帝国cms的connect.php是发现第一句是error_reporting(E_ALL ^ E_NOTICE);以前也没注意过这个语句,知道是设置错误提示的,但不清楚具体怎样设置使用.下面从网 ...
- (转)ThinkPHP Where 条件中使用表达式
转之--http://www.cnblogs.com/martin1009/archive/2012/08/24/2653718.html Where 条件表达式格式为: $map['字段名'] = ...
- loop_login.sh
[oracle@ora10g scripts]$ cat loop_login.sh #/bin/bash####################export ORACLE_BASE=/u01/app ...
- UIViewController的View显示在导航栏下面如何解决?
ios7之前的版本中UIViewController中的view在显示后会自动调整为去掉导航栏的高度的,控件会自动在导航栏以下摆放. 在iOS7中UIViewController的wantsFullS ...
- 【C#学习笔记】一、基础知识
1.1数据类型(P43) 类型 别名 允许的值 sbyte System.SByte -128~127 byte System.Byte 0~255 short System.Int16 -32768 ...