#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. lua的luasocket程序

    -- load namespace local socket = require("socket") -- create a TCP socket and bind it to t ...

  2. Exercise02_15

    import javax.swing.JOptionPane; public class Distance { public static void main(String[] args){ Stri ...

  3. Spring的Aop 注解配置

    1,导包 2,准备目标对象 package com.songyan.anno; public interface UserService { void save(); void delete(); v ...

  4. 【Linux】ubuntu或linux网卡配置/etc/network/interfaces

    转自:http://gfrog.net/2008/01/config-file-in-debian-interfaces-1/   青蛙准备写一个系列文章,介绍一些Debian/Ubuntu里面常用的 ...

  5. debian6 安装VirtualBox的方法

    方法一: 参考: https://www.virtualbox.org/wiki/Linux_Downloads    更新sources.list deb http://download.virtu ...

  6. flask的文件上传和下载

    http://flask.pocoo.org/docs/1.0/api/ http://docs.jinkan.org/docs/flask/api.html?highlight=download h ...

  7. 【angularJS】三个学习angulaJS的链接

    1.官方文档:https://code.angularjs.org/1.5.7/docs/api 2.A Better Way to Learn AngularJS:https://thinkster ...

  8. 【千纸诗书】—— PHP/MySQL二手书网站后台开发之基础知识

    前言: 在具体回顾每一个功能的实现前,还是有必要先温习一些项目涉及到的PHP.MySQL[语法基础].项目github地址:https://github.com/66Web/php_book_stor ...

  9. eclipse No projects are found to import

    导入报:No projects are found to import 新建同名项目,然后删掉 然后:右键项目 根据需要创建资源目录: 最后复制包文件夹分别到这两个资源文件夹里:

  10. C# /windowForm/WPF/SilverLight里面操作Word帮助类提供给大家

    很多的程序都需要用到对word的操作,数据库里面的表需要一书面的形式展示出来,最近在的一个项 using System; using System.Collections.Generic; using ...