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

求必须出去的骑士人数。

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

依次遍历每个双连通分量,但是,对于邻接表中,有一些点不是双连通分量里面的,于是要重新编号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. android主线程ActivityThread

    ActivityThread在Android中它就代表了Android的主线程,但是并不是一个Thread类. 源码如下: http://androidxref.com/6.0.0_r1/xref/f ...

  2. Hadoop Ecosytem

    There are a lot of Hadoop related projects which are open sourced and widely used by many componies. ...

  3. Beam编程系列之Python SDK Quickstart(官网的推荐步骤)

    不多说,直接上干货! https://beam.apache.org/get-started/quickstart-py/ Beam编程系列之Java SDK Quickstart(官网的推荐步骤)

  4. java多线程之join方法使用

    看这篇博客:http://www.cnblogs.com/skywang12345/p/3479275.html

  5. C#下查找并杀死子进程(进程树)

    参考:如何杀死进程及子进程 /** * 传入参数:父进程id * 功能:根据父进程id,杀死与之相关的进程树 */ public static void KillProcessAndChildren( ...

  6. [ElasticSearch] 如何使用中文分詞ik與繁簡轉換stconvert插件

    一. 環境安裝 ElasticSearch(以下簡稱ES)安裝已經算相對簡單了, 但要使用需要配置的插件, 對剛入門的新手還是有點麻煩, 所幸medcl大神提供一個配置好的ES-rtf版本, 新手們在 ...

  7. dpkg: error: dpkg status database is locked by another process 解决方法

    使用dpkg -i/apt命令安装,报错: ------------------------------------------------------------- dpkg: error: dpk ...

  8. Javascript “等于”

    JavaScript支持“=”.“==”和“===”运算符. 我们应当理解这些(赋值.相等.恒等)运算符之间的区别,并在编码过程中小心使用. == equality 等同,用于一般比较,在比较的时候可 ...

  9. FFmpegInterop 库在 Windows 10 应用中的编译使用

    FFmpegInterop 简介 FFmpegInterop 是微软推出的封装 FFmpeg 的一个开源库,旨在方便在 Windows 10.Windows 8.1 以及 Windows Phone ...

  10. SpringSecurity 3.2入门(3)单用户登录

    1.增加web.xml文件配置如下 <!-- 获取Spring Security session的生命周期,这个监听器会在 session 创建和销毁的时候通知 Spring Security ...