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 ...
随机推荐
- PAT_1002 写出这个数
宝宝不开心了.自从回家开始百练就上不去POJ也上不去,今天突然HDU也上不去了,PAT25分的题目都快更新完了.我就按顺序往下面更新了.回学校之后题目质量能高出不少= =. 问题描述: 读入一个自然数 ...
- 理解Http协议(一)
本文对Http协议进行了简要的描述,说明了其用途的广泛性:通过代码对Http连接和Http请求消息的发送进行实现,希望能将这些抽象的过程直观的显示出来:最后对HttpURL和Http协议中“资源”这些 ...
- PHP问题
/usr/bin/ld: cannot find -lltdlcollect2: ld returned 1 exit statusmake: *** [libphp5.la] 错误 1 缺少libt ...
- Javascript执行环境、作用域链
执行环境 可以把执行环境想象为一个圆圈,里面包含了一些变量.函数. 执行环境定义了变量或函数的有权访问的其他数据,决定了它们各自的行为.还有一个顶部执行环境.在浏览器中,顶部执行环境既为window, ...
- sqlserver批量插入数据问题
下午做一个批量添加的功能,自动添加的那种,总是没有达到理想情况,后来请教师傅,给了这个批量添加的代码,一改果然好了,敬佩之情油然而生哈.分享下~ insert into t_ShopApplicati ...
- eval(phpcode) 字符当代码执行
eval(phpcode)eval() 函数把字符串按照 PHP 代码来计算.相当于在字符串两边分别加上PHP语 法标签 该字符串必须是合法的 PHP 代码,且必须以分号结尾. 如果没有在代码字符串中 ...
- 项目规范和建立-从frozenui学习
1.分支branches开发新功能,主干trunk是稳定发布的.因为frozenui下载完,看到branches比trunk多了一个版本 2.版本号定义,主版本.次版本.修订号 大版本号: 主版本号: ...
- php5.3 不支持 session_register() 此函数已启用的解决方法
php从5.2.x升级到5.3.2.出来问题了.有些原来能用的程序报错了,Deprecated: Function session_register() is deprecated php从5.2.x ...
- Swift -- SnapKit
Snapkit SnapKit是专门为Swift语言提供AutoLayout布局使用的,特点为语法简洁易用. let subview = UIView() subview.backgroundColo ...
- 第 4 章 多例模式【Multition Pattern】
以下内容出自:24种设计模式介绍与6大设计原则 这种情况有没有?有!大点声,有没有? 有!,是,确实有,就出现在明朝,那三国期间的算不算,不算,各自称帝,各有各的地盘,国号不同.大家还记得那首诗< ...