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个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...
随机推荐
- 【welcome-file-list】让默认页生效
<welcome-file-list> <welcome-file>404.html</welcome-file> <welcome-file>/vie ...
- Hibernate与数据库分表
数据库分片(shard)是一种在数据库的某些表变得特别大的时候采用的一种技术. 通过按照一定的维度将表切分,可以使该表在常用的检索中保持较高的效率,而那些不常用的记录则保存在低访问表中.比如:销售记录 ...
- javaweb学习总结(三十六)——使用JDBC进行批处理
在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...
- Java多态的体现之接口
/** * * @author Administrator * 功能:接口体现多态 */ package com.test4; public class Test { public static vo ...
- new[]上面居然有一个内存计数,怪不得delete[]从来不出错
开眼界了,留个爪,以后再仔细看几遍: http://www.cnblogs.com/hazir/p/new_and_delete.html
- Yii url createUrl redirect相关
一篇文章: 在yii中明明白白生成网址: 在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,act ...
- ruby 线程学习
i=1 Thread.start{ while true print "Thread 1 \n" i+=1 if i==5 then Thread.kill Thread.curr ...
- 一类最小割bzoj2127,bzoj2132 bzoj3438
思考一下我们接触的最小割问题 最小割的基本问题(可能会和图论的知识相结合,比如bzoj1266,bzoj1797) 最大权闭合图(bzoj1497) 最大点权覆盖集,最大点权独立集(bzoj1324) ...
- WordPress Think Responsive Themes ‘upload_settings_image.php’任意文件上传漏洞
漏洞名称: WordPress Think Responsive Themes ‘upload_settings_image.php’任意文件上传漏洞 CNNVD编号: CNNVD-201311-06 ...
- [HDU 1254] 推箱子
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...