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$个 ...
随机推荐
- mybatis处理查询map列表属性为null的问题,而导致查询map无该key对象
1.常规处理方法(数据库以mysql为例) IFNULL(m.last_use_time,) ) ) as last_lat if判断是否为null,设置一个默认值. 2.前台jsp页面处理,判断是否 ...
- MFC单文档程序结构
MFC单文档程序结构三方面: Doc MainFrame View
- poj 3686
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3791 Accepted: 1631 Descr ...
- iOS音效
//AudioToolbox.framework是一套基于C语言的框架,使用它来播放音效其本质是将短音频注册到系统声音服务(System Sound Service) //System Sound S ...
- Bit-Map
昨日读July大神<教你如何迅速秒杀掉:99%的海量数据处理面试题>博客,有这么一题与大家分享: 给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断 ...
- 配置IIS应用程序池
IIS 6的核心在于工作进程隔离模式,而应用程序池则是定义工作进程如何进行工作,因此,可以说应用程序池是整个IIS 6的核心. 和IIS 5中只能使用单个应用程序池不同,工作在工作进程隔离模式的IIS ...
- 8天学通MongoDB——第一天 基础入门
原文地址:http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html 关于mongodb的好处,优点之类的这里就不说了,唯 ...
- 凌乱的yyy
题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) 所以,他想知道他最多能参加 ...
- 用C语言写个程序推算出是星期几?(用泰勒公式实现)
在日常生活中,我们常常遇到要知道某一天是星期几的问题.有时候,我们还想知道历史上某一天是星期几.比如: “你出生的那一天是星期几啊?” “明年五一是不是星期天?我去找你玩?” 通常,解决这个问题的最简 ...
- OneAPM:打造云时代的应用性能管控平台
在2015年大连市CIO信息化年会的现场,记者与OneAPM东北区总经理佟维针对云时代的企业系统应用性能的管理控制进行了简短交流.北京蓝海讯通科技股份有限公司,即OneAPM是中国基础软件领域的新兴领 ...