题意:给你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的更多相关文章

  1. UVALive - 3523 - Knights of the Round Table

    Problem  UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...

  2. UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)

    尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...

  3. UVALive 3523 : Knights of the Round Table (二分图+BCC)

    题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; ; int n,m; int dfn[maxn],low[ ...

  4. uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)

    题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...

  5. UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)

    由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...

  6. 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)

    Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...

  7. POJ2942 UVA1364 Knights of the Round Table 圆桌骑士

    POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...

  8. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  9. POJ 2942 Knights of the Round Table

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10911   Acce ...

随机推荐

  1. ASP.NET获取上传图片的大小

    1.采用客户端javascript可以取得图片大小 <input id="FileUpload" type="file" size="30&qu ...

  2. starling 中的 EventDispatcher 和 Flash中原生的 EventDispatcher

    starling 比较早之前就有开始解了,但只到最近参与一个用starling 做为框架的手游项目才真正做为一程来使用它. 项目也是刚开始搭建,在这做些笔记. 在写一个管理类时, 遇到 starlin ...

  3. Mysql DOC阅读笔记

    Mysql DOC阅读笔记 转自我的Github Speed of SELECT Statements 合理利用索引 隔离调试查询中花费高的部分,例如函数调用是在结果集中的行执行还是全表中的行执行 最 ...

  4. php 验证码生成方法 及使用

    基本思路是: 在生成图片的页面中(as: yzm.php)1.设置生成的图片的宽度和高度:2.设置图片要写入的字符:3.截取显示在图片上的字符;4.开启session,把上面截取的字符存放在sessi ...

  5. android之调用webservice 实现图片上传

    转:http://www.cnblogs.com/top5/archive/2012/02/16/2354517.html public void testUpload(){ try{ String ...

  6. 快速理解webStroage

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. SVG绘制矩形简单示例分享

    最近我初学HTML5,刚在一步步学习SVG,积累了一些个人心得和程序代码,希望和大家分享,今天分享“svg之矩形”部分 1.简单矩形 效果图如下: 关键代码: <svg xmlns=" ...

  8. 『奇葩问题集锦』Fedora ubuntu 下使用gulp 报错 Error: watch ENOSPC 解决方案

    用gulp启动,错误如下 Error: watch ENOSPC at exports._errnoException (util.js:746:11) at FSWatcher.start (fs. ...

  9. 普及下Oracle hints语法

    普及下Oracle hints的语法:{DELETE|INSERT|SELECT|UPDATE} /*+ hint [text] [hint[text]]... */ 1.hint只能出现在诸如sel ...

  10. bzoj 3065: 带插入区间K小值 替罪羊树 && AC300

    3065: 带插入区间K小值 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1062  Solved: 253[Submit][Status] Des ...