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

求必须出去的骑士人数。

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

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

    初识Mina,简要记录理解内容和实现demo. 这里先简述一下BIO和NIO的区别: 同步阻塞IO(BIO):一个连接一个线程,客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任 ...

  2. linux无敌权限之chattr

    chattr 修改文件或者目录的文件属性,添加超级权限 -R 递归修改文件机子目录 -V 显示详细修改的内容 + 在原有的基础上追加参数 — 减参数 = 跟新为指定参数 a append 设定改参数后 ...

  3. Oracle 基础系列之1.2 oracle的基本使用

    在1.1的安装过程完成之后,进入第二部分,oracle的基本使用,在使用前,首先测试下,oracle是否安装成功,以及检查oracle的关键服务时候开启 1.首先测试下,oracle是否安装成功 打开 ...

  4. 牛客网Java刷题知识点之垃圾回收算法过程、哪些内存需要回收、被标记需要清除对象的自我救赎、对象将根据存活的时间被分为:年轻代、年老代(Old Generation)、永久代、垃圾回收器的分类

    不多说,直接上干货! 首先,大家要搞清楚,java里的内存是怎么分配的.详细见 牛客网Java刷题知识点之内存的划分(寄存器.本地方法区.方法区.栈内存和堆内存) 哪些内存需要回收 其实,一般是对堆内 ...

  5. 【程序员技术练级】学习一门脚本语言 python(三)跟数据库打交道

    接着上一篇,该篇讲述使用python对数据库进行基本的CRUD操作,这边以sqlite3为例子,进行说明.sqlite3 是一个非常轻型的数据库,安装和使用它是非常简单的,这边就不进行讲述了. 在py ...

  6. java url生成二维码保存到本地

    http://blog.sina.com.cn/s/blog_5a6efa330102v1lb.html http://blog.csdn.net/about58238/article/details ...

  7. Mavne 打包时出现程序包找到不的问题

    <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...

  8. 02.for循环

    语法: for(表达式1;表达式2;表达式3) { 循环体; } 练习1: namespace _02.for循环的练习01 { class Program { static void Main(st ...

  9. 【学习笔记】实用类String的基本应用即常用方法

    一.String类概述 在Java中,字符串被作为String类型的对象来处理. String类位于java.lang包中,默认情况下会自动导入到所有的程序中. 创建String对象的方法如下: St ...

  10. Cocos2d-js 开发记录:自定义按钮

    游戏开发总是有些特殊,一般的预制的UI无法满足要求.其实对于不复杂的功能,与其看文档还不如自己写一个.比如游戏中一个虚拟键盘,其中的按键在按下时会增长,变为原来的两倍高度,在原来高度上方显示按键的字如 ...