题目链接: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. Log4j使用教程 (转载http://www.codeceo.com/article/log4j-usage.html)

    日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...

  2. codevs4189字典(字典树)

    /* 本字典树较弱 只支持插入单词 查询单词. 特殊的 bool变量w 标记此字母是不是某个单词的结束 (然而这个题并没卵用) */ #include<iostream> #include ...

  3. ADLINK 8158控制程序-连续运动(VB.NET)

    运动平台:日脉的二维运动平台(一个旋转平台和一个滑动平台) 开发环境:VS2010 + .NET Framework + VB.NET 使用文件:pci_8158.vb motion_8158_2D. ...

  4. datagrid加下拉列表dropdownlist

    datagrid中代码: <asp:datagrid id="dgList" runat="server" ItemStyle-HorizontalAli ...

  5. Windows Azure上的Odoo(OpenERP)-1.创建Ubuntu虚拟机,安装PostgreSQL 数据库

    前提是您必须拥有Windows Azure的账号,如果没有的话,可以去Windows Azure 中国区网站申请免费试用账号.哈哈,我就是第一批申请的试用账号,感觉自己挺幸运的.申请的过程就不写了,请 ...

  6. Animation动画(一)

    Android的animation由四种类型组成:alpha(渐变透明度动画效果).scale(渐变尺寸伸缩动画效果).translate(画面转换位置移动动画效果).rotate(画面转移旋转动画效 ...

  7. Android Studio中关于Project与Module

    在Android Studio中一个Project和Eclipse中的WorkSpace是相似的,而一个Module与Eclipse中的Project是相似的(大致可以这么的认为) 若在Android ...

  8. Linq101-Projection

    using System; using System.Linq; namespace Linq101 { class Projection { /// <summary> /// This ...

  9. css 圆角效果

    http://intacto10years.com/index_start.php<div style="width:800px; height:1300px;">&l ...

  10. 牛顿法与拟牛顿法,DFP法,BFGS法,L-BFGS法

    牛顿法 考虑如下无约束极小化问题: $$\min_{x} f(x)$$ 其中$x\in R^N$,并且假设$f(x)$为凸函数,二阶可微.当前点记为$x_k$,最优点记为$x^*$. 梯度下降法用的是 ...