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个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...
随机推荐
- c++ 重定位输出到DOS
#define USE_WIN32_CONSOLE int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTST ...
- Java练习题
1.实现一个类似于ConcurrentHashMap的分段加锁 import java.util.HashMap; import java.util.Map; import java.util.con ...
- C#基础|面向对象之多态
什么是多态 一句话解释,同一种事物表现出的多种形态. 看完以后,应该还是蒙的,还是看代码吧.. 现有Person类,Student类,Teacher类 其中Person类是Student和Te ...
- 进程间通信(IPC) 简介
IPC是进程间通信的简称.传统上该术语描述的是运行在某个操作系统之上的不同进程间消息传递的不同方式. 我们讨论分为四个领域: 消息传递(管道,FIFO,消息队列(system v消息队列,posix消 ...
- 网络基本功(八):细说TCP滑动窗口
https://community.emc.com/message/842129#842129
- 驱动开发 - WDK 调试及 SVN 环境搭建
由于从公司辞职了,所以以前在公司里搭建的驱动开发环境也就 Game Over 了, 同样由于那环境是很久以前搭建的,自己也有很多记不清楚的地方了, 而且其中还是有很多需要注意的地方的,所以在这里顺便做 ...
- 做最好的自己(Be Your Personal Best)
成功——做最好的自己 价值观——成功源于诚信 积极主动——成功的选择在于自己 同理心——人际交往的基础 自信——用信心放飞自我 自省——在反思中走向成功 勇气——勇往直前的精神 胸怀——海纳百川的境界 ...
- WPF——文本随滚动条改变而改变
一.造一个窗体,拖进一个文本框TextBox和滚动条Slider 二.让文本框的内容随滚动条的滚动而改变,即文本框绑定到滚动条上 三.实现效果
- Asp.net性能优化技巧
[摘 要] 我只是提供我几个我认为有助于提高写高性能的asp.net应用程序的技巧,本文提到的提高asp.net性能的技巧只是一个起步,更多的信息请参考<Improving ASP.NET Pe ...
- 白书P61 - 点集配对问题
白书P61 - 点集配对问题 状压DP #include <iostream> #include <cstdio> #include <cstring> using ...