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 ...
随机推荐
- WCF图片上传
WCF越来越流行,俺也在用,这是废话.项目中遇到需要图片上传,但是wcf上传会遇到一些异常,调试了N久,找了好多个解决方案才最终解决.代码直接贴上了 /// <summary> /// 笔 ...
- sqlserver 理解文件和文件组
在sqlserver中,数据库在硬盘的存储方式和普通文件存储一样,仅仅几个文件而已,sqlserver通过管理逻辑上的文件组的方式来管理存储数据的文件, 如图: 文件组管理着磁盘上的文件,而文件中存放 ...
- ASP.NET在IIS7中如何更改网站的.net framework框架版本
IIS7安装好以后使用了.net 2.0 framework框架,经过折腾发现如下方法可以更改框架版本,从而可以部署使用其他版本框架开发的网站 方法一:建立网站时设置.net框架版本 方法二:对于已经 ...
- Brackets 配置
插件 Brackets Icons 左侧导航的文件图标 FuncDocr 注释工具 QuickDocsJS js帮助文档 Beautify 格式化代码 Brackets Git git支持 ...
- 使用c#生成Identicon图片
Identicon是什么 我们在站点注册的时候通常系统会在我们没有提供自定义头像时为我们指定一个默认的头像,不过,样子千篇一律很是难看.聪明的程序员想了很多办法来解决这个问题,比如你能在这里看到很漂亮 ...
- c# 中模拟一个模式匹配及匹配值抽取
摘一段模式的说明, F#的: msdn是这么描述它的:“模式”是用于转换输入数据的规则.模式将在整个 F# 语言中使用,采用多种方式将数据与一个或多个逻辑结构进行比较.将数据分解为各个构成部分,或从数 ...
- 包含为 HTTP 定义的状态代码的值(枚举)
using System; namespace System.Net { // 摘要: // 包含为 HTTP 定义的状态代码的值. public enum HttpStatusCode { // 摘 ...
- Java I/O继承图
Reader/Writer继承关系图 RandomAccess继承关系图
- Jar mismatch! Fix your dependencies的问题(转)
看到网上有说: 在开发Android项目的时候,有时需要引用多个项目作为library.在引用项目的时候,有时会出现“Jar mismatch! Fix your dependencies”错误. 这 ...
- HTML5之地理信息应用 获取自己的位置
上代码: window.onload = function() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosit ...