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 ...
随机推荐
- boost库----enable_shared_from_this类的作用和实现原理
使用boost库时,经常会看到如下的类 class A:public enable_share_from_this<A> 在什么情况下要使类A继承enable_share_from_thi ...
- 身份证js验证
<script type="text/javascript"> //--身份证号码验证-支持新的带x身份证 function isIdCardNo(num) { var ...
- jQuery DataTables 插件使用笔记
初始化 在页面中 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type=&q ...
- mongoDB单元测试
http://blog.csdn.net/kiwi_coder/article/details/37873093 内存mongo https://github.com/flapdoodle-oss/d ...
- onbeforeunload、beforeunload
<script type="text/javascript"> function addOnBeforeUnload(e) { var ev = e || ev ...
- C# 翻页设计:首页,上一页,下一页,末页 ,跳转
int pageSize = 0; //每页显示行数 int nMax = 0; //总记录数 int pageCount = 0; //页数=总记录数/每页显示行数 int pageCurrent ...
- 【转载】db blocks gets & consistent gets
LOGIC IO(逻辑读次数)= db block gets + consistent gets consistent get : 在一致读模式下所读的快数,包括从回滚段读的快数. db block ...
- NET笔记——Delegate
对于初学者,委托是很容易让人晕的,一是晕它如何起作用,二是晕它有什么用. 最近回过头来又看了下委托,又有些不同的感觉,写之自用. 声明方面,委托可以被声明在类内,也可以与类同级,并且声明时没有方法体: ...
- iPhone不同机型适配 6/6plus --备用
机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片的大小,跟坐标之间有个对应关系,比如1:1或1:2等: ppi:代表屏幕物理大小到图片大小的比例值,如果 ...
- 转:如何制作一个定制的 PHP 基础 Docker 镜像(一)
原文来自于:http://open.daocloud.io/ru-he-zhi-zuo-yi-ge-ding-zhi-de-php-ji-chu-docker-jing-xiang/ 目标:准备一个定 ...