POJ 2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942
题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置。如果意见发生分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防止赞同和反对的票一样多,知道哪些骑士相互憎恨之后,你的任务是统计有多少个骑士不可能参加任何一个会议。
思路 :这个题牵扯的知识点挺多的,具体的可以参考白书上解释的蛮详细的。
#include <iostream>
#include <stdio.h>
#include <stack>
#include <string.h>
#include <algorithm>
#include <vector> using namespace std;
const int maxn = ; struct Edge
{
int u,v ;
Edge(int u ,int v):u(u),v(v) {}
} ; int pre[maxn],dfs_clock ,iscut[maxn],Bcc_cnt,Bccno[maxn] ;
vector<int>G[maxn],Bcc[maxn] ;
stack<Edge>S ;
int odd[maxn],color[maxn] ;
int A[maxn][maxn] ; 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,-) ;
} 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 main()
{
int kase = ,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 Knights of the Round Table(双连通分量)的更多相关文章
- POJ 2942 Knights of the Round Table 黑白着色+点双连通分量
		
题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...
 - poj 2942 Knights of the Round Table  圆桌骑士(双连通分量模板题)
		
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
 - 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 ...
 - POJ 2942 Knights of the Round Table
		
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
 - POJ 2942 Knights of the Round Table - from lanshui_Yang
		
Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels ...
 - poj 2942 Knights of the Round Table(点双连通分量+二分图判定)
		
题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...
 - POJ 2942 Knights of the Round Table (点双连通分量)
		
题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...
 - POJ 2942.Knights of the Round Table (双连通)
		
简要题解: 意在判断哪些点在一个图的 奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...
 - POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)
		
题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...
 - poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
		
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...
 
随机推荐
- hdu 1095 A+B for Input-Output Practice (VII)
			
A+B for Input-Output Practice (VII) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
 - touch——移动端
			
touch事件原生一定要用addEventListener来绑定 一.原生 touchstart:触摸开始时触发 touches:当前位于屏幕上所有手指的列表 event.touches.length ...
 - js 获取url中的查询字符串
			
function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)( ...
 - Android沉浸式状态栏实现
			
Step1:状态栏与导航栏半透明化 方法一:继承主题特定主题 在Android API 19以上可以使用****.TranslucentDecor***有关的主题,自带相应半透明效果 例如: < ...
 - JavaScript学习笔记(2)——JavaScript和DOM的关系
			
文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.DOM实际上是以面向对象方式描述的文档模型.DOM定义了表示和修改文档所需的 ...
 - 修改虚机IP
			
同网段的话,直接修改,不同网段的话,使用以下方法: 步骤一:nova list --all-tenant 找到相应虚拟机+--------------------------------------+ ...
 - 生产者-消费者模型的3种Java实现:synchronized,signal/notifyAll及BlockingQueue
			
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3555111.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
 - 安装Cygwin
			
如果你现在正在学习C语言,而你又不希望使用微软提供的任何C语言的任何编译器,那么你应该考虑一下GCC.GCC是运行于类UNIX系统下的编译器工具集,这又引出了另一个让人头疼的问题,你没有一台现成的装有 ...
 - resid入门笔记(二)
			
本节介绍redis 消息订阅 密码 持久化 主从配置 首先我对消息订阅理解的不深,应该说仅知道概念吧 发送消息 cctv1 发送 hello cctv2 发送 ‘你好’ client ...
 - 客户端(android,ios)与服务器通信
			
android,ios客户端与服务器通信为了便于理解,直接用PHP作为服务器端语言 其实就是一个 http请求响应的过程序,先从 B/S模式说起浏览器发起http请求,服务器响应请求,并把数据返回给浏 ...