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 ...
随机推荐
- MyBatis框架原理1:构建SqlSessionFactory的过程
SqlSessionFactoryBuilder 首先创建了一个SqlSessionFactoryBuilder对象,然后调用该对象的build方法加载全局XML配置的流文件构建出一个SqlSessi ...
- SQL修改数据表字段长度
alter table m_Assysn_t nocheck CONSTRAINT allAlter Table m_Assysn_t ALTER column ppid VARCHAR(150)al ...
- python 并发编程 多线程 开启线程的两种方式
一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 二 开启线程的两种方式 第一种 每造一个进程,默认有一个线程,就是 ...
- Python 爬取煎蛋网妹子图片
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-24 10:17:28 # @Author : EnderZhou (z ...
- private/默认/protected/public权限修饰符的区别
private/默认/protected/public权限修饰符和面向对象的三大特性的封装性有着密切关系.它们都可以修饰类的成员,其中的默认和public还可以修饰类. 类的成员包括:成员变量.成员方 ...
- hdoj4507(数位dp)
题目链接:https://vjudge.net/problem/HDU-4507 题意:定义如果一个整数符合下面3个条件之一,那么我们就说这个整数和7有关—— 1.整数中某一位是7: 2.整数的每一位 ...
- 【转帖】如何看待 HTTP/3 ?
如何看待 HTTP/3 ? https://mp.weixin.qq.com/s/fC10Cyj6xjjwOCnqxX-Dvg 车小胖的公众号 转帖学习一下. 原创: 车小胖谈网络 车小胖谈网络 20 ...
- [转帖]Ubuntu 对应内核版本
带有相应Linux内核版本的Ubuntu版本列表 https://www.helplib.com/ubuntu/article_155943 问题: 是否有带有默认对应的Linux内核版本的Ubu ...
- POJ 2253 Frogger(dijkstra 最短路
POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...
- hibernate update-->参数绑定
Hibernate 更新数据库 参数绑定总结: 一.query.setParameter(属性名,真实值,类型); String hql="update User u set u.userN ...