之前做过这个题目,现在回想起来,又有新的柑橘。

求必须出去的骑士人数。

每一个双连通分量,如果是一个奇圈,那么一定是二分图染色失败。

依次遍历每个双连通分量,但是,对于邻接表中,有一些点不是双连通分量里面的,于是要重新编号bccno,因为割点bccno只有一个值,

但是,他要多次使用,因此要重新编号bccno

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

POJ 2942 圆桌骑士的更多相关文章

  1. POJ 2942 Knights of the Round Table 黑白着色+点双连通分量

    题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...

  2. 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

    [POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

  3. POJ2942 UVA1364 Knights of the Round Table 圆桌骑士

    POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...

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

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

  5. LA 3523 圆桌骑士

    题目链接:http://vjudge.net/contest/141787#problem/A http://poj.org/problem?id=2942 此题很经典 知识点:DFS染色,点-双连通 ...

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

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

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

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

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

  9. poj 2942(点双连通+判奇圈)

    题目链接:http://poj.org/problem?id=2942 思路:我们对于那些相互不憎恨的骑士连边,将每次参加会议的所有人(不一定是整个骑士团,只需人数>=3且为奇数)看做一个点双联 ...

随机推荐

  1. oracle 基础知识(一)

    Oracle 用户.权限.角色管理 01.概念 用户:对数据库的访问需要以适当的身份通过验证,这就是用户的作用:每个Oracle用户都有自己的用户名和密码,并且拥有他们所创建的任意表.视图和其他资源, ...

  2. GreenPlum 大数据平台--外部表(三)

    一,外部表介绍 Greenplum 在数据加载上有一个明显的优势,就是支持数据的并发加载,gpfdisk是并发加载的工具,数据库中对应的就是外部表 所谓外部表,就是在数据库中只有表定义.没有数据,数据 ...

  3. java中创建User Libray

    第一步:右键项目==>Build Path ==>Configure Build Path... 第二步:选择Libraries==>点击 Add Library.. 第三步:选择U ...

  4. linux_api之高级IO

    本篇索引: 1.引言 2.非阻塞IO 3.记录锁(文件锁) 4.io多路复用(I/O multiplexing ) 5.异步IO 6.存储映射IO 1.引言 我们第三篇学习了对IO的open.read ...

  5. shell中if的可判断的类型

    -d :判断制定的是否为目录-z:判断制定的变量是否存在值-f:判断制定的是否为文件-L:判断制定的是否为符号链接-r:判断制定的是否可读-w:判断制定的是否可写-x:判断存在的对象是否可以执行!:测 ...

  6. C# 面试题二

    1.        请编程实现一个冒泡排序算法? int [] array = new int [*] ; ; ; i < array.Length - ; i++) { ; j < ar ...

  7. C# 创建一个WCF服务

    做代码统计,方便以后使用: app.config配置文件设置: <configuration> <system.serviceModel> <bindings> & ...

  8. Spring课程 Spring入门篇 5-2 配置切面aspect

    本节主要讲了在xml中配置切面的demo 1 解析 1.1 配置切面xml 1.2 配置切面xml 1.3 问:什么是动态代理? 2 代码演练 2.1 配置切面xml 1 解析 1.1 配置切面xml ...

  9. BZOJ3033: 太鼓达人(欧拉回路)

    题意 题目链接 Sol 第一问的答案是\(2^M\),因为每个位置只有\(0 / 1\)两种情况,最优情况下一定是每个位置代表着一个长度为\(K\)的字符串 考虑相邻两个字符串之间的转化,第二个字符串 ...

  10. JavaWeb_01_html基本学习

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...