做这题简直是一种折磨。。。

有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 (点的双连通分量)的更多相关文章

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

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

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

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

  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 圆桌骑士(双连通分量模板题)

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

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

  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)任何两个互相仇视的骑士不能相邻,每个骑 ...

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

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

随机推荐

  1. PL/SQL — 存储过程

    存储过程子程序的一种类型,能够完成一些任务,作为schema对象存储于数据库.是一个有名字的PL/SQL代码块,支持接收或不接受参数,同时也支持参数输出.一个存储过程通常包含定于部分,执行部分,Exc ...

  2. javascript debut trick, using the throw to make a interrupt(breakpoint) in your program

    console.log('initialize'); try { throw "breakPoint"; } catch(err) {} when I debug the extj ...

  3. DB天气app冲刺第三天

    昨天很郁闷而且烦躁的的过了一天 什么也没弄.今天其实也没有怎么做..进度非常慢.. 因为个人的问题 所以这两天的效率非常慢. 但今天还是做了一些东西.把listview做出来了.做出了一个按钮的效果. ...

  4. jersey post提交到 ContainerRequestFilter 而HttpServletRequest获取不到数据(转)

     jersey post提交到 ContainerRequestFilter 而HttpServletRequest获取不到数据 问题:在serverfilter request获取不到post提交的 ...

  5. POJ 2375 Cow Ski Area[连通分量]

    题目链接:http://poj.org/problem?id=2375题目大意:一片滑雪场,奶牛只能向相邻的并且不高于他当前高度的地方走.想加上缆车是的奶牛能从低的地方走向高的地方,求最少加的缆车数, ...

  6. uva 12654

    dp,用优先队列存,上个节点节点覆盖下来的长度: 不过还不是很明白: #include<cstdio> #include<queue> #include<algorith ...

  7. sql里条件is null 在thinkphp里

    $map['字段名'] = array('exp',' is NULL'); 譬如:$condition['url']  = array('exp',' is NULL');

  8. loadrunner_analysis技巧_average 和 90% percent

    “90% Percent Time” 表示90%的事务response time 都维持在某个值附近,不是 average response time * 90%;  “Average Time” 平 ...

  9. 如何设置EditPlus的默认编码utf-8方式

    最近在使用英文版的Editplus写代码的时候,发现中文字符在调试过程中都变成了乱码,发现是Editplus编码设置的问题,Editplus默认编码可能是ANSI,需要将其改成UTF-8.干脆给设置默 ...

  10. MySQL的SQL_CALC_FOUND_ROWS

    分页程序一般由两条SQL组成: SELECT COUNT(*) FROM ... WHERE .... SELECT ... FROM ... WHERE LIMIT ... 如果使用SQL_CALC ...