#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. Eclipse的workspace中放入Ext JS卡死或OOM的解决方案

    Eclipse的workspace中放入Ext JS卡死或OOM的解决方案 原因:是由于Ext JS 的所有的文件js验证 方法一:关于Eclipse解决Ext JS卡死方案: 打开Eclipse的w ...

  2. P2P通信标准协议(四)之SIP

    在前面几篇文章中我们介绍了建立p2p通信的一般协议(簇),以及一种完整的NAT传输解决方案ICE, 但是对于多用户的通信情况,还有一些通用协议来实现标准化的管理,如之前讲过的SDP和SIP等,SIP( ...

  3. Delphi 通过SQLite3, SQLiteTable3 操作数据库

    var sql, sFile:string; db:TSQLiteDatabase;begin try sFile := G_AppPath + CH_IPC712Db; //if FileExist ...

  4. sql获取汉字的拼音首字母的函数

    ql获取汉字的拼音首字母   if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and ...

  5. inner join, left join ,right join 结果

    假设有两个表结构如下: 表table1 表 table 2 内连接: --内连接 select * from table1 inner join table2 on table1.ID = table ...

  6. Linux下获取本机IP地址的代码

    Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...

  7. javascript快速入门25--浏览器中的XML

    打开XML 首先,直接从浏览器中打开XML文件,浏览器会对其进行格式良好性检查,如果不符合XML语法规范则显示出错,如果格式良好,再检查是否包含样式表(CSS或XSL),如果包含样式表,则用样式表格式 ...

  8. javascript通过url向jsp页面传递中文参数乱码解决方法

    解决方法:在传递参数前将中文参数进行两次编码,jsp页面获取参数后对中文参数进行一次解码,中文参数就不会变为乱码了! 参考例子: <%@ page language="java&quo ...

  9. 当客户端为RemoteAnywhere时Chef-server 使用knife-windows bootstrap的一个问题

    笔者在使用knife-windows bootstrap 一个安装了RemoteAnywhere的节点遇到一个坑: knife bootstrap 192.168.1.245 -r 'role[my_ ...

  10. 【转】angular中$parse详解教程

    原文: https://yq.aliyun.com/ziliao/40516 ------------------------------------------------------------- ...