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 ...
随机推荐
- 哈希表 HashTable(又名散列表)
简介 其实通过标题上哈希表的英文名HashTable,我们就可以看出这是一个组合的数据结构Hash+Table. Hash是什么?它是一个函数,作用可以通过一个公式来表示: index = HashF ...
- 写一个比较全的进制转换函数--ic
//写一个比较全的进制转换函数-----未完成 #include <stdio.h> //D进制转换后 (比如10-2进制) 结果可能会很大 需要很长的字符串来存 #include < ...
- oop理论
三大特性: 封装:把对象的属性和行为独立的一个整体,并尽可能的隐藏对象内部实现细节.增加安全性. 继承:从已有的类中派生出新的类,称为子类,子类继承父类的属性和行为,并能根据自己的需求扩展出新的行为. ...
- P2085 最小函数值
题目链接hhh:https://www.luogu.org/problemnew/show/P2085好嘛,运气真好,刚A掉序列合并,正好碰到这题,可以说是序列合并的升级版了 那么简单说一下思路,首先 ...
- python基础语法之字符串
1 字符串中*的使用 *可以使字符串重复n次 print('hello world ' * 2) # hello world hello world 2 索引获取字符串的字符元素 print('hel ...
- Metinfo5.1 /message/access.php SQL注入漏洞
- POJ3585 Accumulation Degree【换根dp】
题目传送门 题意 给出一棵树,树上的边都有容量,在树上任意选一个点作为根,使得往外流(到叶节点,叶节点可以接受无限多的流量)的流量最大. 分析 首先,还是从1号点工具人开始$dfs$,可以求出$dp[ ...
- mybati代码生成器 mybatis-generator
Mybatis代码生成器,用于快速生成代码 代码 https://github.com/wangxinforme/mybatis-generator
- linux free 命令 查看内存使用情况
查看Linux服务器下的内存使用情况,可以使用命令free -m [root@localhost ~]$ free // 以KB为单位显示内存使用情况 [root@localhost ~]$ free ...
- Spring(八)-- 代理设计模式
代理设计模式 1:基本概念 2:JDK动态代理 1. 创建接口 2. 创建实现类 3. 创建代理类 /** * jdk动态代理 不能满足 继承父类的情况 * * AnimalProxy 代理类 */ ...