uva 3523 Knights of the Round Table
题意:给你n,m n为有多少人,m为有多少组关系,每组关系代表两人相互憎恨,问有多少个骑士不能参加任何一个会议。
白书算法指南
对于每个双联通分量,若不是二分图,就把里面的节点标记
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <stack>
using namespace std;
const int maxn=;
int pre[maxn],iscut[maxn],bccno[maxn],dfs_clock,bcc_cnt;
int A[maxn][maxn];
vector<int> G[maxn],bcc[maxn];
struct Edge {
int u,v;
};
stack<Edge>S; int dfs(int u,int fa) {
int lowu=pre[u]=++dfs_clock;
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;
bcc_cnt++;
bcc[bcc_cnt].clear();
for(;;) {
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]=;
return lowu;
} void find_bcc(int n) {
memset(pre,,sizeof(pre));
memset(iscut,,sizeof(iscut));
memset(bccno,,sizeof(bccno));
dfs_clock=bcc_cnt=;
for(int i=; i<n; i++) {
if(!pre[i])
dfs(i,-);
}
} int odd[maxn],color[maxn];
bool bipartite(int u,int b) {
for(int i=; i<G[u].size(); i++) {
int v=G[u][i];
if(bccno[v]!=b)
continue;
if(color[v]==color[u])
return false;
if(!color[v]) {
color[v]=-color[u];
if(!bipartite(v,b))
return false;
}
}
return true;
} int main() {
int kase =,m,n;
while(scanf("%d%d",&n,&m)==&&n) {
for(int i=; i<n; i++) {
G[i].clear();
}
memset(A,,sizeof(A));
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]) {
G[u].push_back(v);
G[v].push_back(u);
}
}
}
find_bcc(n);
memset(odd,,sizeof(odd));
for(int i=; i<=bcc_cnt; i++) {
memset(color,,sizeof(color));
for(int j=; j<bcc[i].size(); j++) {
bccno[bcc[i][j]]=i;
}
int u=bcc[i][];
color[u]=;
if(!bipartite(u,i)) {
for(int j=; j<bcc[i].size(); j++) {
odd[bcc[i][j]]=;
}
}
}
int ans=n;
for(int i=; i<n; i++) {
if(odd[i])
ans--;
}
printf("%d\n",ans);
}
}
uva 3523 Knights of the Round Table的更多相关文章
- UVALive - 3523 - Knights of the Round Table
Problem UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...
- UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)
尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...
- UVALive 3523 : Knights of the Round Table (二分图+BCC)
题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; ; int n,m; int dfn[maxn],low[ ...
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...
- UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
- 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: 10911 Acce ...
随机推荐
- 桶排序之python实现源码
tmp = [] def bucket_sort(old): for i in range(len(old)): tmp.append([]) for i in old: tmp[int( i * l ...
- 【原创】Linux下获取命令的帮助与常用命令
Linux中的shell命令一般是执行步骤:用户在终端输入命令回车,系统内核会在当前用户的环境变量PATH中去读取环境变量的值 变量的值就是命令的路径,命令路径不只一个,于是系统会从这些路径中从左至右 ...
- 【转】Oracle中dual表的用途介绍
原文:Oracle中dual表的用途介绍 [导读]dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情. dual是一个虚拟表, ...
- CSS3鼠标移入移出图片生成随机动画
今天分享使用html+css3+少量jquery实现鼠标移入移出图片生成随机动画,我们先看最终效果图(截图为静态效果,做出来可是动态的哟) 左右旋转 上下移动 缩放 由于时间关系我就不一步步解析各段代 ...
- Inspiron 14 7000 系列 (7447) 游匣14 拆机图
Inspiron 14 7000 系列 (7447) 游匣14 拆机图 游匣配置不多说,i5起步,标配4G GTX850M显卡,这么霸道的配置给我玩扫雷肯定不卡.配置高功耗就大,不过游匣的散热 ...
- Windows server2008 搭建ASP接口访问连接oracle数据库全过程记录--备用
真的是太不容易了,以前的时候在window server 2003上面搭建了一套asp+oracle的接口系统,就费了好大的劲儿,其实那会迷迷瞪瞪的也不知道怎么的就弄好了,也懒得管了.OK,从昨天到今 ...
- WebApp开发:ajax请求跨域问题的解决
服务端:PHP 客户端:Andorid, HTML5, jQuery, ajax 现象:本想通过jQuery的ajax功能从服务器取回数据存到手机的缓存里,结果总是错误,后来想到可能是跨域问题,所以查 ...
- XMLHttpRequest2的进步之处
本文参考自:XMLHttpRequest2 新技巧 (重点保留demo,方便自己日后查阅) HTML5是现在web开发中的热点,虽然关于web app和local app一直有争论,但是从技术学习的角 ...
- HTML5 知识点
HTML5 知识点 (1)语义化标记 <header>,<footer>,<nav>,<article>,<section> ...
- POJ 2528 Mayor's posters(线段树)
点我看题目 题意 :建一堵墙粘贴海报,每个候选人只能贴一张海报,海报的高度与墙一样高,一张海报的宽度是整数个单位,墙被划分为若干个部分,每个部分的宽度为一个单位,每张海报完全的覆盖一段连续的墙体,墙体 ...