UVA-1364.Knights of the Round Table 无向图BCC
题目链接:https://vjudge.net/problem/UVA-1364
题意:有n个人参加会议,互相憎恨的人不能坐在相邻的位置,并且每个会议参加的人数必须是奇数,求有多少个人不能参加任何一个会议。
思路:如果两个人可以坐在一起,则在他们之间建立一条无向边。求不在任何一个简单奇圈上面的点的个数。简单圈上面的点必然属于同一个点双联通分量,因此首先需要找出所有的点双联通分量、因为二分图是没有奇圈的,所以需要求那些不是二分图的点双联通分量。虽然这些点双联通分量一定含有奇圈,那么是否是所有的点都在奇圈上面呢。v属于点双联通分量B,但是不在属于B的奇圈C上面。根据点双联通的性质,v一定可以到达C中的一个结点u1,v也一定可以到达C中的入一个结点u2,在C中u1到u2的两条路的长度一奇一偶,总能构建出一个奇圈。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<stack>
#include<vector>
using namespace std;
const int MAXN=1e3+,INF=0x3f3f3f3f,MOD=1e9+;
int n,m;
int vis[MAXN][MAXN];
vector<int>G[MAXN];
int dfs_color=; ///dfs时间戳
int pre[MAXN],post[MAXN];
int bcc_cnt=; ///联通分量
int low[MAXN]; ///u及其后代所能连回的最早祖先的pre值
int iscut[MAXN]; ///割点
vector<pair<int,int> >birdge; ///桥
struct edge
{
int u,v;
};
stack<edge>S;
int bccno[MAXN]; ///点所在的双联通分量
vector<int>bcc[MAXN]; ///双联通分量
int dfs(int u,int fa)
{
int lowu=pre[u]=++dfs_color;
int child=;
for(int i=; i<G[u].size(); i++)
{
int v=G[u][i];
edge e=(edge)
{
u,v
};
if(!pre[v])
{
S.push(e);
child++;
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u])
{
iscut[u]=true;
if(lowv>pre[u]) birdge.push_back(make_pair(u,v));
bcc_cnt++;
bcc[bcc_cnt].clear();
while(!S.empty())
{
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<&&child==) iscut[u]=;
low[u]=lowu;
return low[u];
}
void find_bcc()
{
bcc_cnt=;
dfs_color=;
memset(pre,,sizeof(pre));
memset(iscut,,sizeof(iscut));
memset(bccno,,sizeof(bccno));
for(int i=; i<=n; i++)
if(!pre[i]) dfs(i,-);
}
void init(int n,int m)
{
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++) G[i].clear();
birdge.clear();
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
vis[u][v]=vis[v][u]=;
}
for(int i=; i<=n; i++)
{
for(int j=; j<i; j++)
{
if(vis[i][j]) continue;
G[i].push_back(j);
G[j].push_back(i);
}
}
}
int color[MAXN];
int odd[MAXN];
bool bipartite(int u,int d)
{
for (int i = ; i < G[u].size(); i++)
{ int v=G[u][i];
if (bccno[v]!=d) continue;
if (color[v]==color[u]) return false;
if (!color[v])
{
color[v]=-color[u];
if (!bipartite(v,d)) return false;
}
}
return true;
}
int solve()
{
memset(odd,,sizeof(odd));
for(int i=; i<=bcc_cnt; i++)
{
for(int j=;j<bcc[i].size();j++) bccno[bcc[i][j]]=i;
memset(color,,sizeof(color));
color[bcc[i][]]=;
if(!bipartite(bcc[i][],i))
{
for (int j=; j<bcc[i].size(); j++)
odd[bcc[i][j]]=;
}
}
int ans=;
for(int i=; i<=n; i++)
if(!odd[i]) ans++;
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if(n==&&m==) break;
init(n,m);
find_bcc();
cout<<solve()<<endl;
}
return ;
}
无向图BCC+二部图
UVA-1364.Knights of the Round Table 无向图BCC的更多相关文章
- UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)
尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...
- uva 3523 Knights of the Round Table
题意:给你n,m n为有多少人,m为有多少组关系,每组关系代表两人相互憎恨,问有多少个骑士不能参加任何一个会议. 白书算法指南 对于每个双联通分量,若不是二分图,就把里面的节点标记 #include ...
- UVAlive3523 Knights of the Round Table(bcc)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18122 [思路] 点-双连通分量 求出bcc,判断每个bcc是否为 ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
- POJ 2942 Knights of the Round Table - from lanshui_Yang
Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
随机推荐
- webuploader.js
PHP 多图上传,图片批量上传插件,webuploader.js,百度文件上传插件(案例教程) WebUploader作用: 使用WebUploader还可以批量上传文件.支持缩略图等等众多参数选项 ...
- MATLAB 移动复制文件
copyfile('source','destination'):%复制文件 delete('fileName');%删除文件 movefile('source','destination');%移动 ...
- kali下的miranda工具只适合同一路由下使用
在终端输入如下命令: miranda -v -i eth0 上面的命令是指定打开网卡eth0,返回结果如下: miranda提示输入开启upnp的主机,现在我们不知道哪台主机开启了upnp,输入命令“ ...
- ettercap的使用
ettercap -i eth0 -T -M arp:remote -q /<网关地址>// /<目标地址>// arp:remote ,表示双向 使用图形化界面 etterc ...
- break
public class b { public static void main(String[] args) { int i=0; for(;i<=10;i++){ if (i&g ...
- Jmeter(十九) Md5加密操作之-------BeanShell PreProcessor(转载)
转载自 http://www.cnblogs.com/yangxia-test 背景: 有一些登录会做一些md5校验,通过jmeter的BeanShell可以解决MD5加密情况. 1.首先需要一个解码 ...
- I/O复用之epoll
epoll 简介 epoll是为处理大批量句柄而作了改进的poll,它是在2.5.44内核中被引进的. 相关函数调用 int epoll_create(int size); 创建一个epoll的句柄. ...
- springboot 缓存
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- IDEA中配置JUnit单元测试
参考安装教程:https://www.jianshu.com/p/c37753b6dbd6 如果想用junit4的话,需要在pom.xml中配置. 需要安装JUnitGenerator V2.0插件, ...
- vi 基本命令使用
vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对 Unix及Linux系统的任何版本,vi编辑器是完全 ...