LINK


题目大意

有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件:

1.人数大于1

2.总人数是奇数

3.有矛盾的人不能相邻

问有多少人不能和任何人形成任何的环

思路

发现如果在原图上直接判断非常的不方便

考虑在补图上挖掘性质

补图:

补图和原图没有任何一条重边

不图和原图的所有边并集是一个完全图

即删去所有存在的边,把不存在的边加上

发现可以选出来的环在补图上一定是一个奇环

那么就可以考虑怎么找到补图中的所有奇环

  • 性质1:包含一个奇环的点双连通分量中的每一个点一定属于某一个奇环

这个性质还挺神奇的

因为这个点双联通分量中的每两个点之间至少有两条点不相交路径

所以假设一个偶环包含了一个奇环中的一部分\(p_u...p_v\),

因为p是奇环所以\(p_u\)到\(p_v\)中一定有一条长度是奇数的路径和一条长度是偶数的路径

所以结论成立咯

所以就可以发现只需要对每个强连通分量判断包不包含奇环就可以了

  • 性质2:包含奇环的充分必要条件是二分图染色冲突

还挺好证明的吧

如果二分图染色不出现冲突,就是个二分图了

二分图中是没有奇环的

所以直接点双联通判一下有没有奇环就可以了


毒瘤poj上有多组数据

注意清零的问题

特别是二分图染色的数组


//Author: dream_maker
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 1e3 + 10;
const int M = 1e6 + 10;
struct Edge{
int u, v, nxt;
} E[M << 1];
int g[N][N], n, m, in[N];
int head[N], tot, vis[N], col[N];
void add(int u, int v) {
++tot;
E[tot].u = u;
E[tot].v = v;
E[tot].nxt = head[u];
head[u] = tot;
}
void clean() {
memset(head, 0, sizeof(head));
tot = 0;
}
bool dfs(int u, int now) {
col[u] = now;
for (int i = head[u]; i; i = E[i].nxt) {
int v = E[i].v;
if (!in[v]) continue;
if (col[v] && col[v] == col[u]) return 1;
else if (!col[v] && dfs(v, 3 - now)) return 1;
}
return 0;
}
namespace Tarjan {
int dfn[N], low[N], bel[N];
int ind = 0, cnt_bcc = 0;
stack<Edge> st;
vector<int> bcc[N];
void init() {
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(bel, 0, sizeof(bel));
ind = 0;
}
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++ind;
for (int i = head[u]; i; i = E[i].nxt) {
int v = E[i].v;
if (v == fa) continue;
if (!dfn[v]) {
st.push(E[i]);
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
++cnt_bcc;
bcc[cnt_bcc].clear();
Edge now;
do {
now = st.top(); st.pop();
if (bel[now.u] != cnt_bcc) {
bel[now.u] = cnt_bcc;
bcc[cnt_bcc].push_back(now.u);
}
if (bel[now.v] != cnt_bcc) {
bel[now.v] = cnt_bcc;
bcc[cnt_bcc].push_back(now.v);
}
} while (now.u != u || now.v != v);
fv(j, bcc[cnt_bcc]) in[bcc[cnt_bcc][j]] = 1;
if (dfs(bcc[cnt_bcc][0], 1))
fv(j, bcc[cnt_bcc]) vis[bcc[cnt_bcc][j]] = 1;
fv(j, bcc[cnt_bcc]) in[bcc[cnt_bcc][j]] = col[bcc[cnt_bcc][j]] = 0;
}
} else {
if (dfn[v] < dfn[u]) {
st.push(E[i]);
low[u] = min(low[u], dfn[v]);
}
}
}
}
}
using namespace Tarjan;
int main() {
freopen("input.txt", "r", stdin);
while (1) {
Read(n), Read(m);
if (!n && !m) return 0;
tot = 0;
fu(i, 1, n) head[i] = vis[i] = 0;
init();
fu(i, 1, n)
fu(j, 1, n)
g[i][j] = (i == j);
fu(i, 1, m) {
int u, v; Read(u), Read(v);
g[u][v] = g[v][u] = 1;
}
fu(i, 1, n)
fu(j, 1, n)
if (!g[i][j])
add(i, j);
fu(i, 1, n) if (!dfn[i]) tarjan(i, 0);
int ans = n;
fu(i, 1, n) if (vis[i]) --ans;
Write(ans), putchar('\n');
}
return 0;
}

POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】的更多相关文章

  1. 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)

    圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...

  2. POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)

    题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...

  3. POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug

    题面还好,就不描述了 重点说题解: 由于仇恨关系不好处理,所以可以搞补图存不仇恨关系, 如果一个桌子上面的人能坐到一起,显然他们满足能构成一个环 所以跑点双联通分量 求点双联通分量我用的是向栈中pus ...

  4. UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)

    尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...

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

  6. 「题解」:[POJ2942]Knights of the Round Table

    问题 E: Knights of the Round Table 时间限制: 1 Sec  内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...

  7. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

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

  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. Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定

    题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...

随机推荐

  1. mysql主从复制,及扩展

    一.MySQL简单复制相关概念: 1. mysql复制的意义:Mysql复制是使得mysql完成高性能应用的前提 2. mysql复制的机制: SLAVE端线程: IO thread: 向主服务请求二 ...

  2. Cannot find entry file index.android.js in any of the roots:[ Android ]

    Changed the version of react project to a lower one from here npm install -g rninit rninit init [Pro ...

  3. 2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)

    1. 通常,你可能有一个计算的属性依赖于数组中的所有元素来确定它的值.例如,你可能想要计算controller中所有todo items的数量,以此来确定完成了多少任务. export default ...

  4. 2.6 The Object Model -- Bindings

    一个binding在两个属性之间创建一个链接,当一个改变时,另外一个被自动更新为一个新的值. bindings可以在同一个对象中连接两个属性,或者用在两个不同的对象中. 不像大多数框架一样包含某种形式 ...

  5. Redis快速入门之简介

    一.Redis是什么? Redis 是一个开源(BSD许可)基于内存数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如字符串(strings), 散列(hashes) ...

  6. 向Oracle 数据表中插入一条带有日期类型的数据

    有一张表:batch(批次表) 表的字段如下: 第一种情况: 现在需要插入一条当前的系统时间 sql 如下: insert into batch (batch_id, cus_id, batch_nu ...

  7. 浅谈location对象

    简介 Location 对象存储在 Window 对象的 Location 属性中,表示那个窗口中当前显示的文档的 Web 地址.通过Location对象,可以获取URL中的各项信息,调用对象方法也可 ...

  8. FFmpeg 入门(7):Seeking

    本文转自:FFmpeg 入门(7):Seeking | www.samirchen.com 处理 seek 命令 我们将为播放器添加 seek 的能力.这个过程中,我们会看到 av_seek_fram ...

  9. 20145211MSF基础应用实验

    20145211MSF基础应用实验 一.实验博客 ms08_067攻击实验 http://www.cnblogs.com/entropy/p/6690301.html ms12_004漏洞攻击 htt ...

  10. Linux基础入门(实验楼实验)

    实验一 Linux系统简介 Linux和windows.Mac OS一样是一种操作系统.最早流行起来的操作系统是UNIX,但由于其过度商业化,价格昂贵,因此在校园里人们大多选择MINIX.1991年, ...