题目来源:POJ 2942 Knights of the Round Table

题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条件表示2个人直接有矛盾

思路:求补图  能够坐在一起 就是能够相邻的人建一条边 然后假设在一个奇圈上的都是满足的 那些不再不论什么一个奇圈的就是不满足 求出全部奇圈上的点 总数减去它就是答案

首先有2个定理

1.一个点双连通分量是二分图 你就没有奇圈 假设有奇圈  那就不是二分图  充分必要条件 所以

2.一个点双连通分量有奇圈  那这个点双连通分量全部点都在奇圈上

所以这题就求点双连通分量 然后对每一个分量进行二分图判定 不是二分图 就把该双连通分量全部点都标记  标记是由于一个割点可能属于多个双连通分量

所以标记在求和

#include <vector>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 1010;
struct Edge
{
int u, v;
Edge(){}
Edge(int u, int v): u(u), v(v){}
};
vector <int> a[maxn];
vector <int> bcc[maxn];
int pre[maxn];
int low[maxn];
int bccno[maxn];
int dfs_clock;
int bcc_cnt;
int bri;
int n, m;
int degree[maxn];
stack <int> S;
int A[maxn][maxn];
int odd[maxn];
int color[maxn];
bool bipartite(int u, int b)
{
for(int i = 0; i < a[u].size(); i++)
{
int v = a[u][i];
if(bccno[v] != b)
continue;
if(color[u] == color[v])
return false;
if(!color[v])
{
color[v] = 3 - color[u];
if(!bipartite(v, b))
return false;
}
}
return true;
}
void dfs(int u, int fa)
{
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int i = 0; i < a[u].size(); i++)
{
int v = a[u][i];
if(v == fa)
continue;
if(!pre[v])
{
dfs(v, u);
low[u] = min(low[u], low[v]);
if(pre[u] <= low[v])
{
bcc_cnt++;
while(1)
{
int x = S.top(); S.pop();
bccno[x] = bcc_cnt;
bcc[bcc_cnt].push_back(x);
if(x == v)
{
bcc[bcc_cnt].push_back(u);
break;
}
}
}
}
else if(v != fa)
low[u] = min(low[u], pre[v]);
}
/*if(low[u] == pre[u])
{
bcc_cnt++;
while(1)
{
int x = S.top(); S.pop();
bccno[x] = bcc_cnt;
bcc[bcc_cnt].push_back(x);
if(x == u)
break;
}
}*/
}
void find_bcc()
{
memset(pre, 0, sizeof(pre));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = bri = 0;
for(int i = 1; i <= n; i++)
if(!pre[i])
dfs(i, -1);
}
int main()
{ while(scanf("%d %d", &n, &m) && (n||m))
{
memset(A, 0, sizeof(A));
while(m--)
{
int u, v;
scanf("%d %d", &u, &v);
A[u][v] = A[v][u] = 1;
}
for(int i = 0; i <= n; i++)
{
a[i].clear();
bcc[i].clear();
}
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
{
if(A[i][j])
continue;
a[i].push_back(j);
a[j].push_back(i);
}
find_bcc();
memset(odd, 0, sizeof(odd));
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 = n;
for(int i = 1; i <= n; i++)
if(odd[i])
ans--;
printf("%d\n", ans);
}
return 0;
}

POJ 2942 Knights of the Round Table 黑白着色+点双连通分量的更多相关文章

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

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

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

  3. POJ 2942 Knights of the Round Table

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

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

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

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

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

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

  7. poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)

    #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...

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

    http://poj.org/problem?id=2942 题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...

  9. POJ 2942.Knights of the Round Table (双连通)

    简要题解: 意在判断哪些点在一个图的  奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...

随机推荐

  1. net core服务器缺包,如何在线安装?

    Install -package命令不行. 下面命令也不行.求助大家,怎么安装?

  2. HTTP 协议基本知识

    HTTP协议    7.1.什么是HTTP协议:        HTTP协议是用来规定浏览器客户端和服务器通信的方式 7.2.基本原则        基于请求响应模型        一次请求对应一次响 ...

  3. Codeforces 811 B. Vladik and Complicated Book

    B. Vladik and Complicated Book   time limit per test 2 seconds memory limit per test 256 megabytes i ...

  4. 6、Django实战第6天:用户登录

    今天开始,我们需要来写后台逻辑了.... 后台逻辑代码都是编写在views.py文件里面,今天要完成的登录功能,因此来编辑users.views.py 这里我们根据请求方法来判断分为2种情况,网页默认 ...

  5. [Codeforces 8D] Two Friends

    Brief Introduction: 有两人a.b,他们都在A点,a经过B点到C点,而b直接到C点.a走过的距离不超过la,b走过距离不超过lb,询问他们可能经过最长的公共距离. Algorithm ...

  6. POJ 3068 "Shortest" pair of paths(费用流)

    [题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...

  7. 【博弈论】poj2348 Euclid's Game

    假设当前b>a. 一.b%a==0 必胜 二.b<2*a,当前我们没有选择的余地,若下一步是必胜(最终能到情况一),则当前必败:反之,当前必胜. 三.b>2*a,假设x是使得b-ax ...

  8. 求一个整数个位数之和 Exercise06_02

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求一个整数个位数之和 * */ public class Exercise ...

  9. 动态OSPF配置路由表

    动态ospf设置路由表 以Rourer1为例子 (1)首先设置路由器端口ip Router(config)#inter f0/0 Router(config-if)#ip add 192.168.1. ...

  10. JavaScript学习系列之执行上下文与变量对象篇

    一个热爱技术的菜鸟...用点滴的积累铸就明日的达人 正文 在上一篇文章中讲解了JavaScript内存模型,其中有提到执行上下文与变量对象的概念.对于JavaScript开发者来说,理解执行上下文与变 ...