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$个 ...
随机推荐
- jQuery1.9.1--attr,prop与val方法源码分析
这里只介绍这几个方法的源码,这部分引用了一个技巧,钩子对象,用来做兼容fixed的对象,后面也有一些使用.钩子对象具体的兼容细节这里就不详解了. var nodeHook, boolHook, rcl ...
- 深入JS第一天:原型和它的小伙伴们(一)
我在这里不说定义,找点问题,再解决问题. 一.原型 Q1:这样做输出的结果是什么? jQuery= String; jQuery.prototype.say = function () { alert ...
- hdu 4725
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- poj3415 Common Substrings(后缀数组,单调栈 | 后缀自动机)
[题目链接] http://poj.org/problem?id=3415 [题意] A与B长度至少为k的公共子串个数. [思路] 基本思想是将AB各个后缀的lcp-k+1的值求和.首先将两个字符串拼 ...
- iOS开发--CornerStone上传静态库(.a文件)
首先打开软件左上角 CornerStone-Preferences-SubVersion 第一个地方把对号去掉,第二个地方把.a那个删除,然后save. 然后把你的.a文件放到本地的相应文件夹下, 但 ...
- ntelliJ IDEA 14 注册码
user or company nameo license key63625-MQ87K-3SRZ2-8MQYB-6NQZC-2Z8K6
- 关闭Centos写磁盘功能
一个Linux文件默认有3个时间.atime:对此文件的访问时间. ctime:此文件inode发生变化的时间. mtime:此文件的修改时间. 如果有多个小文件(比如Web服务器的页面上有多个小图片 ...
- 移动设备中导入gdb调试工具
(1)概述 接ADB调试桥安装(方式一),ADB调试桥安装好了后一般的移动设备内都不含有gdb工具, 要想使用gdb工具可以借助adb的push参数进行上传. gdb分为gdb客户端和服务端,文件可以 ...
- Android 消息广播Intent传递数据
1.创建布局文件activity_broadcast.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- jsp中的out对象 和 servlet中的response.getOutputStream()
web容器生成的servlet代码中有out.write(””),这个和JSP中调用的response.getOutputStream()产生冲突. 即Servlet规范说明,不能既调用 respon ...