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,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...
随机推荐
- BaseAdapter以及对ListView的优化(转)
背景 对于ListView.GridView.Gallery.Spinner等等,它是它们的适配器,直接继承自接口类Adapter的,使用BaseAdapter时需要重写很多方法,其中最重要的当属ge ...
- 使用EditText搜索listview里面的内容,实现Listview跟随变动的情况
1.布局的XML文件里面添加EditText控件(省略)控件id=mSearch ListView的id=admin_lv; 2.一.获取ListView展示的数据(通过适配器获取) 二.这个是我要说 ...
- cxf WebService整理 (基于注解)
http://blog.csdn.net/zjw10wei321/article/details/39889823
- UIView -> image & 本地时间获取
//UIView 转换为图片 UIGraphicsBeginImageContext(self.rootsView.bounds.size); [_rootsView.layer renderInCo ...
- Swift - 17 - 数组的初始化
import UIKit // 声明数组 var array = ["A", "B", "C", "D", " ...
- angular自定义指令详解
指令(directive)是angular里面最核心也是最难懂的东西,在慕课网看了下大漠穷秋老湿的视频,自己百度半天做了一些小test,总算把一切都搞明白了. 先列出学习来源: 指令中controll ...
- 微信小应用vs progressive-web-apps
https://developers.google.com/web/progressive-web-apps/
- AutoIt3初探(1)
AutoIt3可实现系统操作,键盘鼠标模拟,是自动化测试的一个好工具. 这个是在线帮助文档,http://www.jb51.net/shouce/autoit/ 需要先下载一个autoIt安装,然后将 ...
- Android源码下载方法详解
转自:http://www.cnblogs.com/anakin/archive/2011/12/20/2295276.html Android源码下载方法详解 相信很多下载过内核的人都对这个很熟悉 ...
- 切换samba用户
打开cmd 输入命令 net use \\192.168.xxx.xxx\IPC$ /DELETE 192.168.xxxx.xxx是linux的ip地址