#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
using namespace std; const int maxn = 1000 + 10;
//const int INF = 0x3f3f3f3f;
int n, m;
int A[maxn][maxn]; //计算点—双连通分量。用栈S来保留当前bcc中的边
int pre[maxn], iscut[maxn], bccno[maxn], dfs_clock, bcc_cnt;
vector<int> G[maxn], bcc[maxn];
struct Edge {
int u, v;
Edge(int u = 0, int v = 0) : u(u), v(v) {
}
};
stack<Edge> S;
int dfs(int u, int fa) {
int lowu = pre[u] = ++dfs_clock;
int child = 0;
for(int i = 0; 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(); //注意! bcc从1開始编号
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 < 0 && child == 1) iscut[u] = 0;
return lowu;
}
void find_bcc(int n) {
memset(pre, 0, sizeof(pre));
memset(iscut, 0, sizeof(iscut));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = 0;
for(int i = 0; i < n; i++) {
if(!pre[i]) dfs(i, -1);
}
} //dfs给二分图进行黑白二着色,用颜色1表示黑色,颜色2表示白色,0表示没着色
int color[maxn]; //推断节点u所在的连通分量是否为二分图
bool bipartite(int u, int id) {
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(bccno[v] != id) continue;
if(color[v] == color[u]) return false;
if(!color[v]) {
color[v] = 3 - color[u];
if(!bipartite(v, id)) return false;
}
}
return true;
} int odd[maxn];
void init() {
int u, v;
memset(A, 0, sizeof(A));
memset(odd, 0, sizeof(odd));
while(m--) {
scanf("%d%d", &u, &v);
u--; v--;
A[u][v] = A[v][u] = 1;
}
for(int i = 0; i < n; i++) G[i].clear();
for(u = 0; u < n; u++)
for(int v = u + 1; v < n; v++)
if(!A[u][v]) G[u].push_back(v), G[v].push_back(u);
} void solve() {
find_bcc(n);
for(int i = 1; i <= bcc_cnt; i++) {
memset(color, 0, sizeof(color));
for(int j = 0; j < bcc[i].size(); j++) bccno[bcc[i][j]] = i;
int u = bcc[i][0];
color[u] = 1;
if(!bipartite(u, i)) {
for(int j = 0; j < bcc[i].size(); j++) odd[bcc[i][j]] = 1;
}
}
int ans = 0;
for(int i = 0; i < n; i++) if(!odd[i])
ans++;
cout << ans << endl;
} int main() {
//freopen("input.txt", "r", stdin);
while(scanf("%d%d", &n, &m) == 2 && n) {
init();
solve();
}
return 0;
}

题意:有n个骑士开会,每次至少三个人且人数必须为奇数,相互憎恨的人不能相邻,给出骑士们相互的憎恨关系。求多少骑士不能參加不论什么一个会议。

大白书经典例题。写完以后感觉收获非常大。

1.以骑士为节点建立无向图,假设两个骑士不憎恨,那么在他们之间连一条边,则题目转化为求不在任一简单奇圈上的结点个数。

2.简单圈上的全部节点必定属于同一双连通分量,因此须要先找出全部双连通分量。

3.非常重要的一个结论!。!!。!

!!对于一个双连通分量来说,假设他是二分图,那么是没有奇圈的。反之有奇圈的图一定不是二分图,证明略。

4.也非常重要的一个结论。!。!!!

。假设结点v所属的一个双连通分量不是二分图。那么v一定属于一个奇圈。如今证明这个结论,假设双连通分量B不是一个二分图,依据3,B中一定含有一个奇圈,那么对于不属于这个奇圈的v来说。依据双连通性,一定存在两条不相交路径(除起点外无公共结点),使得v能到达这个奇圈上的两个不同点,设为v1。v2.

而由于v1和v2在一个不同的奇圈上,那么从v1到v2的两条路径长度一奇一偶。所以总能构造出一条经过v的奇圈。

5.注意。每一个割顶属于多个双连通分量。可能被标记多次。

poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)的更多相关文章

  1. POJ 2942 Knights of the Round Table (点双连通分量)

    题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...

  2. POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)

    题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...

  3. POJ 2942 Knights of the Round Table 黑白着色+点双连通分量

    题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...

  4. poj 2942 Knights of the Round Table - Tarjan

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

  5. POJ 2942 Knights of the Round Table

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

  6. 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 ...

  7. poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)

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

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

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

  9. poj 2942 Knights of the Round Table(点双连通分量+二分图判定)

    题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...

随机推荐

  1. 【MySQL笔记】mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法

    step1:查看 1.1 Mysql命令行里输入"show engines:"查看innoddb数据引擎状态, 1.2 show variables "%_buffer% ...

  2. Scala零基础教学【21-40】

    第24讲:Scala中SAM转换实战详解 SAM:single abstract method 单个抽象方法   我们想传入一个函数来指明另一个函数具体化的工作细节,但是重复的样板代码很多. 我们不关 ...

  3. Redis的安装与idea中的使用

    一.Redis的安装 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开 ...

  4. asp.net 二级域名(路由方式实现)

    自从微软发布 ASP.NET MVC 和routing engine (System.Web.Routing)以来,就设法让我们明白你完全能控制URL和routing,只要与你的application ...

  5. RAISERROR语句

    生成错误消息.RAISERROR 可以引用 sys.messages 目录视图中存储的用户定义消息,也可以动态建立消息.该消息作为服务器错误消息返回到调用应用程序,或返回到 TRY…CATCH 构造的 ...

  6. [Linux]nginx tomcat做负载均衡

    之前使用nginx做过web反向代理,没有做过负载均衡,今天有个同学须要做tomcat的负载均衡,我也研究下. 一共是2个机器,一个物理机(win7)上面部署2个tomcat,使用不同的port启动. ...

  7. 如何用路由器改成WiFi Pineapple系统镜像网络流量

    本文主要介绍利用自己现有的设备,如何制作和使用WiFi Pineapple镜像网络流量,利用DWall模块分析用户数据,然后根据自己的需求,给DWall加入了日志记录功能.最后介绍了如何防范wifi ...

  8. oracle 10g函数大全--聚合函数

    AVG([distinct|all]x) [功能]统计数据表选中行x列的平均值. [参数]all表示对所有的值求平均值,distinct只对不同的值求平均值,默认为all 如果有参数distinct或 ...

  9. Vue基础知识总结(二)

    一.解决网速慢的时候用户看到花括号标记 (1)v-cloak,防止闪烁,通常用于比较大的选择器上. 给元素添加属性v-cloak,然后style里面:[v-cloak]{display:none;} ...

  10. 深入理解JS函数作用域链与闭包问题

    function fun(n,o) { console.log(o) return { fun:function(m){ return fun(m,n); } }; } ); a.fun(); a.f ...