UVALive 3523 : Knights of the Round Table (二分图+BCC)
题意及题解参见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)的更多相关文章
- UVALive - 3523 - Knights of the Round Table
Problem UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...
- UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...
- uva 3523 Knights of the Round Table
题意:给你n,m n为有多少人,m为有多少组关系,每组关系代表两人相互憎恨,问有多少个骑士不能参加任何一个会议. 白书算法指南 对于每个双联通分量,若不是二分图,就把里面的节点标记 #include ...
- 【POJ2942】Knights of the Round Table(二分图 点双联通分量)
题目链接 大意 给定\(N\)个点与\(M\)个关系,每个关系表示某两个点间没有直接的边相连,求不在所有奇环上的点的个数. (\(1\le N\le 1e3,1\le M\le 1e6\)) 思路 考 ...
- UVAlive3523 Knights of the Round Table(bcc)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18122 [思路] 点-双连通分量 求出bcc,判断每个bcc是否为 ...
- UVA-1364.Knights of the Round Table 无向图BCC
题目链接:https://vjudge.net/problem/UVA-1364 题意:有n个人参加会议,互相憎恨的人不能坐在相邻的位置,并且每个会议参加的人数必须是奇数,求有多少个人不能参加任何一个 ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
随机推荐
- 使用SpringWebFlow
使用SpringWebFlow 本章主要内容: · 创建会话式的Web应用程序 · 定义流程状态和行为 Spring Web Flow 是Spring MVC 的扩展,它支持开发基于流程的应用程序.它 ...
- ubuntu中写sh脚本
批量执行命令 https://jingyan.baidu.com/article/3052f5a121c8ac97f21f8661.html 批量执行脚本也是可行的! 如,main.sh内写 sh ...
- P1936 【水晶灯火灵】
lalala~~(才不会告诉你这是题面呢) 这题确实有点坑,第一遍穷举超时,然后就开始了漫漫找规律之路... 终于,在经过5分钟的纠结之后,我终于发现了这个神奇的规律,那就是 Fabonacci!!! ...
- CF1187E Tree Painting【换根dp】
题目传送门 题意 一棵$N$个节点的树,初始时所有的节点都是白色,第一次可以选择任意一个把它涂成黑色.接下来,只能把与黑色节点原来相连的白色节点涂成黑色(涂成黑色的点视为被删去,与其它节点不相连).每 ...
- lua基础学习(三)
一.lua函数 1.在Lua中,函数是对语句和表达式进行抽象的主要方法.既可以用来处理一些特殊的工作,也可以用来计算一些值.Lua 提供了许多的内建函数,你可以很方便的在程序中调用它们,如print( ...
- Dubbo从入门到精通
1.在Dubbo中注解的使用 2.Dubbo启动时qos-server can not bind localhost:22222错误解决 3.Dubbo配置方式详解
- [转帖]Ubuntu 18.04 server安装图形界面及realvnc远程桌面连接
Ubuntu 18.04 server安装图形界面及realvnc远程桌面连接 https://blog.csdn.net/networken/article/details/88938304 转帖 ...
- Matcher和Pattern总结
Matcher.Pattern简介 java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pat ...
- c# 动态加载和卸载DLL程序集
原文:c# 动态加载和卸载DLL程序集 在 C++中加载和卸载DLL是一件很容易的事,LoadLibrary和FreeLibrary让你能够轻易的在程序中加载DLL,然后在任何地方卸载.在 C#中我们 ...
- 13.AutoMapper 之映射前后(Before and After Map Action)
https://www.jianshu.com/p/1ff732094f21 映射前后(Before and After Map Action) 你可能偶尔需要在映射发生前后执行自定义逻辑.这应该很少 ...