题目链接

题意及题解参见lrj训练指南

#include<bits/stdc++.h>
using namespace std; const int maxn=1e3+;
int n,m;
int dfn[maxn],low[maxn],time_tag;
int bccno[maxn],bcc_cnt;
int iscut[maxn];
int A[maxn][maxn];
vector<int> adj[maxn];
vector<int> bcc[maxn];
int odd[maxn];
struct Edge
{
int u,v;
Edge() {}
Edge(int u_,int v_)
{
u=u_,v=v_;
}
};
stack<Edge> st;
void init()
{
memset(A,,sizeof(A));
memset(dfn,,sizeof(dfn));
memset(iscut,,sizeof(iscut));
memset(bccno,,sizeof(bccno));
memset(odd,,sizeof(odd));
for(int i=; i<=n; i++)
adj[i].clear(),bcc[i].clear();
bcc_cnt=time_tag=;
}
void dfs(int u,int pre)
{
low[u]=dfn[u]=++time_tag;
int child=; //子节点数目
for(int v:adj[u])
{
if(v==pre) continue;
if(!dfn[v]) // 把dfn[]当vis[]使用
{
st.push(Edge(u,v));
child++;
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u])
{
iscut[u]=;
bcc_cnt++;
while()
{
Edge x=st.top();st.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(dfn[v]<dfn[u])
{
st.push(Edge(u,v));
low[u]=min(low[u],dfn[v]);
}
}
if(pre<&&child==) iscut[u]=; //只有一个孩子的根节点
}
void find_bcc()
{
for(int i=;i<n;i++)
if(!dfn[i]) dfs(i,-);
} int color[maxn];
//判定结点u所在的连通分量是否为二分图
bool bipartite(int u,int tag)
{
for(int v:adj[u])
{
if(bccno[v]!=tag) continue;
if(color[v]==color[u]) return false;
if(!color[v])
{
color[v]=-color[u];
if(!bipartite(v,tag)) return false;
}
}
return true;
} int main()
{
while(scanf("%d%d",&n,&m)>&&n)
{
init();
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]) adj[u].push_back(v),adj[v].push_back(u);
find_bcc();
for(int i=;i<=bcc_cnt;i++)
{
memset(color,,sizeof(color));
for(int u:bcc[i]) bccno[u]=i;
int u=bcc[i][];
color[u]=;
if(!bipartite(u,i))
for(int v:bcc[i]) odd[v]=;
}
int ans=n;
for(int i=;i<n;i++) if(odd[i]) ans--;
printf("%d\n",ans);
}
}

UVALive 3523 : Knights of the Round Table (二分图+BCC)的更多相关文章

  1. UVALive - 3523 - Knights of the Round Table

    Problem  UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...

  2. uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)

    题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...

  3. UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)

    由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...

  4. uva 3523 Knights of the Round Table

    题意:给你n,m n为有多少人,m为有多少组关系,每组关系代表两人相互憎恨,问有多少个骑士不能参加任何一个会议. 白书算法指南 对于每个双联通分量,若不是二分图,就把里面的节点标记 #include ...

  5. 【POJ2942】Knights of the Round Table(二分图 点双联通分量)

    题目链接 大意 给定\(N\)个点与\(M\)个关系,每个关系表示某两个点间没有直接的边相连,求不在所有奇环上的点的个数. (\(1\le N\le 1e3,1\le M\le 1e6\)) 思路 考 ...

  6. UVAlive3523 Knights of the Round Table(bcc)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18122 [思路] 点-双连通分量 求出bcc,判断每个bcc是否为 ...

  7. UVA-1364.Knights of the Round Table 无向图BCC

    题目链接:https://vjudge.net/problem/UVA-1364 题意:有n个人参加会议,互相憎恨的人不能坐在相邻的位置,并且每个会议参加的人数必须是奇数,求有多少个人不能参加任何一个 ...

  8. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

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

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

随机推荐

  1. UML_2_浅谈UML的概念和模型之UML九种图

    转载:https://my.oschina.net/zhumenzhongren/blog/667353 上文我们介绍了,UML的视图,在每一种视图中都包含一个或多种图.本文我们重点讲解UML每种图的 ...

  2. Ubuntu虚拟机共享文件夹的1234

    第一 在虚拟机内添加路径 进入虚拟机软件,点开工具栏上方虚拟机,点击设置,选择选项,查看共享文件夹,点击添加,下一步 第二: 第三 点击启用此共享 点击完成 第四 查看共享的文件 在mnt里可以看到S ...

  3. 【HANA系列】SAP HANA行列转换

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA行列转换   前 ...

  4. 浅谈 MySQL的预编译

    之前的一篇 Mybatis中 #{}和${}的区别 中涉及到通过 SQL预编译和 #{} 传值 的方式防止SQL注入. 由此引发了想了解预编译的想法.那么什么是预编译那? 一.三个阶段: 词法和语义解 ...

  5. 双系统正确卸载Ubuntu系统

    双系统正确卸载Ubuntu系统  安装系统后由于显卡驱动问题,无法开机,从而只能卸载重装,重装过程如下. 第一步:下载需要的工具包,这里我用的是MBRfix, 可以直接从我分享的网盘链接下载,密码gw ...

  6. SSM004/工作内容

    一.java执行sql脚本 参考博客:java调用SQL脚本执行的方案 1.Service层代码:目的随spring容器启动即执行 //Service层代码 @Component public cla ...

  7. 四、Kubernetes_V1.10集群部署-master-创建kubeconfig

    1.生成配置文件 # 创建 TLS Bootstrapping Token # export BOOTSTRAP_TOKEN=$( /dev/urandom | od -An -t x | tr -d ...

  8. POJ - 1287 Networking (最小生成树&并查集

    You are assigned to design network connections between certain points in a wide area. You are given ...

  9. next_permutation() 全排列函数

    next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...

  10. python 9*9乘法口诀 猜数字游戏