题目来源:POJ 2942 Knights of the Round Table

题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条件表示2个人直接有矛盾

思路:求补图  能够坐在一起 就是能够相邻的人建一条边 然后假设在一个奇圈上的都是满足的 那些不再不论什么一个奇圈的就是不满足 求出全部奇圈上的点 总数减去它就是答案

首先有2个定理

1.一个点双连通分量是二分图 你就没有奇圈 假设有奇圈  那就不是二分图  充分必要条件 所以

2.一个点双连通分量有奇圈  那这个点双连通分量全部点都在奇圈上

所以这题就求点双连通分量 然后对每一个分量进行二分图判定 不是二分图 就把该双连通分量全部点都标记  标记是由于一个割点可能属于多个双连通分量

所以标记在求和

#include <vector>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 1010;
struct Edge
{
int u, v;
Edge(){}
Edge(int u, int v): u(u), v(v){}
};
vector <int> a[maxn];
vector <int> bcc[maxn];
int pre[maxn];
int low[maxn];
int bccno[maxn];
int dfs_clock;
int bcc_cnt;
int bri;
int n, m;
int degree[maxn];
stack <int> S;
int A[maxn][maxn];
int odd[maxn];
int color[maxn];
bool bipartite(int u, int b)
{
for(int i = 0; i < a[u].size(); i++)
{
int v = a[u][i];
if(bccno[v] != b)
continue;
if(color[u] == color[v])
return false;
if(!color[v])
{
color[v] = 3 - color[u];
if(!bipartite(v, b))
return false;
}
}
return true;
}
void dfs(int u, int fa)
{
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int i = 0; i < a[u].size(); i++)
{
int v = a[u][i];
if(v == fa)
continue;
if(!pre[v])
{
dfs(v, u);
low[u] = min(low[u], low[v]);
if(pre[u] <= low[v])
{
bcc_cnt++;
while(1)
{
int x = S.top(); S.pop();
bccno[x] = bcc_cnt;
bcc[bcc_cnt].push_back(x);
if(x == v)
{
bcc[bcc_cnt].push_back(u);
break;
}
}
}
}
else if(v != fa)
low[u] = min(low[u], pre[v]);
}
/*if(low[u] == pre[u])
{
bcc_cnt++;
while(1)
{
int x = S.top(); S.pop();
bccno[x] = bcc_cnt;
bcc[bcc_cnt].push_back(x);
if(x == u)
break;
}
}*/
}
void find_bcc()
{
memset(pre, 0, sizeof(pre));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = bri = 0;
for(int i = 1; i <= n; i++)
if(!pre[i])
dfs(i, -1);
}
int main()
{ while(scanf("%d %d", &n, &m) && (n||m))
{
memset(A, 0, sizeof(A));
while(m--)
{
int u, v;
scanf("%d %d", &u, &v);
A[u][v] = A[v][u] = 1;
}
for(int i = 0; i <= n; i++)
{
a[i].clear();
bcc[i].clear();
}
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
{
if(A[i][j])
continue;
a[i].push_back(j);
a[j].push_back(i);
}
find_bcc();
memset(odd, 0, sizeof(odd));
for(int i = 1; i <= bcc_cnt; i++)
{
memset(color, 0, sizeof(color));
for(int j = 0; j < bcc[i].size(); j++)
bccno[bcc[i][j]] = i;
int u = bcc[i][0];
color[u] = 1;
if(!bipartite(u, i))
{
for(int j = 0; j < bcc[i].size(); j++)
odd[bcc[i][j]] = 1;
}
}
int ans = n;
for(int i = 1; i <= n; i++)
if(odd[i])
ans--;
printf("%d\n", ans);
}
return 0;
}

POJ 2942 Knights of the Round Table 黑白着色+点双连通分量的更多相关文章

  1. poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)

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

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

  3. POJ 2942 Knights of the Round Table

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

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

  5. poj 2942 Knights of the Round Table(点双连通分量+二分图判定)

    题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...

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

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

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

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

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

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

  9. POJ 2942.Knights of the Round Table (双连通)

    简要题解: 意在判断哪些点在一个图的  奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...

随机推荐

  1. jQuery中操作样式

    操作行间样式 // 获取div的样式 $("div").css("width"); $("div").css("color&quo ...

  2. 分析函数调用堆栈的原理和Delphi实现

    来自:http://blog.163.com/liuguang_123/blog/static/816701920105262543890/ ----------------------------- ...

  3. solr requestHandler

    使用哪一个handler: 1. 可以通过在url中追加有名字的handler(以 ' / ' 开头命名)的名称来指定使用哪一个handler. 如: <requestHandler name= ...

  4. CentOS6.8-minimal安装gnome桌面 安装NVC远程桌面连接

    https://blog.csdn.net/nimasike/article/details/72844403

  5. asp.net中利用JSON进行增删改查中运用到的方法

    //asp.net中 利用JSON进行操作, //增加: //当点击“增加链接的时候”,弹出增加信息窗口,然后,在窗体中输入完整信息,点击提交按钮. //这里我们需要考虑这些:我会进行异步提交,使用j ...

  6. csu1216( Trie )

    csu1216 题意 给定一些数,求这些数中两个数的异或值最大的那个值. 分析 转化成二进制数存入字典树,比如说要查询 \(0011\) ,显然和 \(1100\) 结合最优,所以我们直接在字典树上寻 ...

  7. 区间DP【p2858】[USACO06FEB]奶牛零食Treats for the Cows

    Description 约翰经常给产奶量高的奶牛发特殊津贴,于是很快奶牛们拥有了大笔不知该怎么花的钱.为此,约翰购置了N(1≤N≤2000)份美味的零食来卖给奶牛们.每天约翰售出一份零食.当然约翰希望 ...

  8. small test on 5.30 night T2

    (题面写错了,应该是一条从b -> a 的边) 让我们设状态 (a,b,c) 表示存在一个点k,使得  dist(k,b) - dist(k,a) * 2 + 3 = c,显然这里的第三维可以压 ...

  9. 【2-SAT】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League

    先反复地扫(不超过n次),把所有可以确定唯一取法的给确定下来. 然后对于剩下的不能确定的,跑2-SAT.输出可行解时,对于a和¬a,如果a所在的强连通分量序号在¬a之前,则取a,否则不取a.如果a和¬ ...

  10. 【权值分块】bzoj3570 DZY Loves Physics I

    以下部分来自:http://www.cnblogs.com/zhuohan123/p/3726306.html 此证明有误. DZY系列. 这题首先是几个性质: 1.所有球质量相同,碰撞直接交换速度, ...