poj2942 Knights of the Round Table 双连通分支 tarjan
题解:http://blog.csdn.net/lyy289065406/article/details/6756821
讲的很详细我就不多说了。
题目连接:http://poj.org/problem?id=2942
代码(完全白书上的)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <stack>
#define loop(s,i,n) for(i = s;i < n;i++)
#define cl(a,b) memset(a,b,sizeof(a))
const int maxn = ;
using namespace std;
int pre[maxn],iscut[maxn],bccno[maxn],dfsclock,bcc_cnt;
vector<int>g[maxn],bcc[maxn];
struct edge
{
int u,v,w;
};
stack<edge> st; int dfs(int u,int fa)
{
int lowu= pre[u]=++dfsclock;
int child=;
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
edge e;
e.u=u,e.v=v;
if(!pre[v])
{
st.push(e);
child++;
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u])
{
iscut[u]=;
bcc_cnt++;
bcc[bcc_cnt].clear();
for(;;)
{
edge x=st.top();st.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)
{
st.push(e);
lowu=min(lowu,pre[v]);
}
}
if(fa<&&child==)iscut[u]=;
return lowu;
}
void find_bcc(int n)
{
int i;
memset(pre,,sizeof(pre));
cl(iscut,);
cl(bccno,);
dfsclock = bcc_cnt = ;
loop(,i,n)
if(!pre[i]) dfs(i,-);
// puts("yes");
// printf("%d """"""\n",bcc_cnt);
}
int odd[maxn],color[maxn];
bool bipartite(int u,int b)
{
int i;
loop(,i,g[u].size())
{
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 map[maxn][maxn];
int main()
{
int i,kase = ,n,m; while(scanf("%d %d",&n,&m))
{
if(!n)
break;
memset(map,,sizeof(map)); loop(,i,n) g[i].clear(); loop(,i,m)
{
int u,v;
scanf("%d %d",&u,&v);
u--,v--;
map[u][v] = map[v][u] =;
}
int j;
loop(,i,n)
{
loop(i+,j,n)
{
if(!map[i][j])
g[i].push_back(j),g[j].push_back(i);
}
} find_bcc(n); // cout<<bcc_cnt<<endl; 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 ;
}
poj2942 Knights of the Round Table 双连通分支 tarjan的更多相关文章
- POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】
LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...
- 「题解」:[POJ2942]Knights of the Round Table
问题 E: Knights of the Round Table 时间限制: 1 Sec 内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- POJ2942 Knights of the Round Table 点双连通分量,逆图,奇圈
题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先 ...
- POJ2942:Knights of the Round Table
传送门 点双练习. 很简单的一道模板题,建立反图,求出点双,二分图判定奇环. //POJ 2942 //by Cydiater //2016.11.2 #include <iostream> ...
- POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)
题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...
- POJ2942 Knights of the Round Table 点双连通分量 二分图判定
题目大意 有N个骑士,给出某些骑士之间的仇恨关系,每次开会时会选一些骑士开,骑士们会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件:1.任意相互憎恨的两个骑士不能相邻.2.开会人数为大于2的奇 ...
- [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)
建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点 ...
- $POJ2942\ Knights\ of\ the\ Round\ Table$ 图论
正解:图论 解题报告: 传送门! 一道,综合性比较强的题(我是萌新刚学$OI$我只是想练下$tarjan$,,,$QAQ$ 考虑先建个补图,然后现在就变成只有相互连边的点不能做邻居.所以如果有$K$个 ...
随机推荐
- 编写单例的 dojo class
define([ "dojo/_base/declare" ],function( declare ){ var TimeChartService = declare(" ...
- Ckeditor 的加载顺序
我们的只用在文件里面引用一个CKEditor的js文件--CKEditor目录下的ckeditor.js文件, 该文件会完成后续的所有的CKEidtor依赖的js文件的加载. 所依赖的js文件加载顺序 ...
- Sqli-labs less 15
Less-15 本关没有错误提示,那么我们只能靠猜测进行注入.这里我直接从源代码中看到了sql语句 @$sql="SELECT username, password FROM users W ...
- POJ 2591 1338 2545 2247(数列递归衍生问题,思路挺妙)
四道题的难度: 2591<1338<2545<2247 POJ 2591 Set Definition: 这是从discuss里看来的,写的挺好,直接copy,根据我的代码稍有改动( ...
- ls 知识点
ls -R 将会以目录的形式列出所有文件,-S 以文件的大小列出所有文件,-t 将会按照修改时间来列出文件,-i 会显示文件的inode
- php用fsockopen实现post提交数据并获得返回数据
/** * 函数介绍: 用于post方式提交数据 * 输入参数: 完整url, 数据 * 返回值 : 接口返回值 */ function post_it($url, $data = '', $time ...
- 【hdu3579-Hello Kiki】拓展欧几里得-同余方程组
http://acm.hdu.edu.cn/showproblem.php?pid=3579 题解:同余方程组的裸题.注意输出是最小的正整数,不包括0. #include<cstdio> ...
- PHP Simple HTML DOM解析器
一直以来使用php解析html文档树都是一个难题.Simple HTML DOM parser 帮我们很好地解决了使用 php html 解析 问题.可以通过这个php类来解析html文档,对其中的h ...
- 快速构建自己的CentOS发行版
一.制作LTOS具体过程 光盘结构介绍 * isolinux 目录存放光盘启动时的安装界面信息 * images 目录包括了必要的启动映像文件 * CentOS 目录存放安装软件包及信息 * .dis ...
- 图解TCP/IP读书笔记(三)
第三章.数据链路 数据链路层是计算机网络最基本的内容. 数据链路层的协议定义了通过通信媒介互连的设备之间传输的规范. 一.数据链路相关技术 1.MAC地址 关于MAC地址的几个要点: ①MAC地址长度 ...