poj 2942--Knights of the Round Table (点的双连通分量)
做这题简直是一种折磨。。。
有n个骑士,骑士之间相互憎恨。给出骑士的相互憎恨的关系。 骑士要去开会,围成一圈坐,相互憎恨的骑士不能相邻。开会骑士的个数不能小于三个人。求有多少个骑士不能开会。
注意:会议可以开无数次,也就是说一个骑士其实是可以开多次会议的,所以一共可以开会的人也未必是奇数。
求出相互并不憎恨的骑士的关系图,也就是相连的骑士可以挨着。这样如果有一个奇数圈就可以确定一圈的人全部可以参加会议。
性质:如果一个双连通分量内的某些顶点在一个奇圈中(即双连通分量含有奇圈),那么这个双连通分量的其他顶点也在某个奇圈中
又知道二分图染色可以判断奇数圈,所以对每个点的强连通分量求二分图染色就ok了。这题的关键在于求点的双连通分量。
/*********************************************
Memory: 4752 KB Time: 1141 MS
Language: G++ Result: Accepted
*********************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#define pk puts("kkk");
using namespace std; const int N = 1005;
const int M = N * N; struct Edge {
int to, next;
} edge[M];
int head[N];
int cnt_edge;
void add_edge(int u, int v)
{
edge[cnt_edge].to = v;
edge[cnt_edge].next = head[u];
head[u] = cnt_edge++; edge[cnt_edge].to = u;
edge[cnt_edge].next = head[v];
head[v] = cnt_edge++;
} int dfn[N], low[N], idx;
int stk[N], top;
int kind[N], cnt;
bool ok[N], in[N]; int color[N]; int mp[N][N];
int n; bool dfs_color(int u, int c)
{
color[u] = c;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (in[v])
{
if (!color[v] && !dfs_color(v, 3 - c)) return false;
else if (color[v] == color[u]) return false;
}
}
return true;
} void dfs(int u, int pre)
{
dfn[u] = low[u] = ++idx;
stk[++top] = u;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (v == pre) continue;
if (!dfn[v])
{
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) // u是割点, 求双连通分量
{
memset(in, 0, sizeof in);
memset(color, 0, sizeof color);
cnt = 0;
int x;
int num = 0;
do {
x = stk[top--];
kind[cnt++] = x;
in[x] = true;
num++;
} while (x != v);
if (num <= 1) continue;
in[u] = true;
if (!dfs_color(u, 1))
{
ok[u] = true;
while (cnt--) { ok[ kind[cnt] ] = true;}
}
}
}
else low[u] = min(low[u], dfn[v]);
}
} void solve()
{
int ans = 0;
for (int i = 1; i <= n; ++i) dfs(i, -1);
for (int i = 1; i <= n; ++i) if (ok[i]) ans++;
printf("%d\n", n - ans);
} void init()
{
memset(head, -1, sizeof head);
memset(dfn, 0, sizeof dfn);
memset(ok, 0, sizeof ok);
memset(mp, 0, sizeof mp);
cnt_edge = top = idx = 0;
} int main()
{
int m;
int u, v;
while (~scanf("%d%d", &n, &m))
{
if (n == 0 && m == 0) break;
init();
for (int i = 1; i <= m; ++i)
{
scanf("%d%d", &u, &v);
mp[u][v] = mp[v][u] = 1;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i != j && !mp[i][j]) add_edge(i, j);
solve();
} return 0;
}
poj 2942--Knights of the Round Table (点的双连通分量)的更多相关文章
- POJ 2942 Knights of the Round Table (点双连通分量)
题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...
- POJ 2942 Knights of the Round Table 黑白着色+点双连通分量
题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...
- 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 ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- 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 ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- poj 2942 Knights of the Round Table(点双连通分量+二分图判定)
题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...
- POJ 2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...
随机推荐
- SDC(5)–FPGA系统级同步输入的约束
此时计算最大/最小输入延时时,是不应该考虑FPGA本身的tSU/tH的,因为数据进来之后不一定马上送到DFF. 例如: 此时约束如下:
- C# - 自动属性
使用自动属性时,只能通过属性访问数据,不能通过底层私有字段,因为我们不知道底层私有字段的名称,是编译期间定义的.所以也没法对字段赋值或取值进行校验或限制. 无法使用这种方式定义只读或只写属性.
- Ubuntu14.04下Unity桌面托盘图标显示问题
本来想丰富一下功能,遂开始安装大开眼界:Ubuntu下10个厉害的Indicator小程序这里的Indicator小程序. 很不幸,在安装到indicator-multiload的时候,准备注销看一下 ...
- python参考手册--第10、11章执行环境、调试
1.解释器选项和环境 python [options] [-c cmd | filename | - ] [args] 例如:python -m pdb test.py 以脚本的形式运行库模块pdb ...
- [Gauss]HDOJ3976 Electric resistance
题意: 一看图就明白了 要求的是1与n端点间的等效电阻 重点在于转化成考虑电流 根据KCL定理:在任一瞬间流出(流入)该节点的所有电流的代数和恒为零 U = IR 可以令1点的电势为零 那么n点的电势 ...
- c++ 学习笔记 c++ 引用C库注意点:#ifdef __cplusplus 倒底是什么意思?
时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #en ...
- ActionBar官方教程(2)选主题让应用支或不支持ActionBar及支持ActionBar的应用如何隐藏和显示
Adding the Action Bar As mentioned above, this guide focuses on how to use the ActionBar APIs in the ...
- XBMC 最新版本错误
1. Syntax error: "(" unexpected 修改 tools/depends下的makefile.include 将NDK_VER=0x9d
- wzplayer for android V1.6.1 (支持音视频加密播放)
1.更新 2013-11-25: 1.6.1 修复1.6.0版本对rk版本的支持. 以往版本: 1.6.0 1)1.6.0修改了所有默认音频渲染使用AudioTrack输出,这样只要不播放视频,能支持 ...
- poj棋盘分割(记忆化)
http://poj.org/problem?id=1191 黑书上P116 想了挺久 没想出来 想推出一公式来着 退不出来.. 想偏了 正解:递归 #include <iostream> ...