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 ...
随机推荐
- 通过SPList Definition自定义ListItem打开编辑详细页面
在SharePoint中对列表SPList的操作里,常常希望当增加一个条目后还希望关联做一些其它的操作 而SharePoint自身提供的Item操作页面却没有那么灵活:所以通常情况下,我们专门为其提供 ...
- spark下统计单词频次
写了一个简单的语句,还没有优化: scala> sc. | textFile("/etc/profile"). | flatMap((s:String)=>s.spli ...
- spring3.2.0与mybatis3.2.7整合出错--Failed to read candidate component class--nested exception is java.lang.IllegalArgumentException
错误信息如下: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate com ...
- oracle中存储过程的使用
存储过程 刚开始我接触到数据库的时候,感觉存储过程是很难的,但是当你看完我给你列举的例子,你就能够轻松的掌握存储过程的创建和使用了. 存储过程是在大型数据库系统中存储过程在数据库中经过第一次编译后就不 ...
- ### Paper about Event Detection
Paper about Event Detection. #@author: gr #@date: 2014-03-15 #@email: forgerui@gmail.com 看一些相关的论文. 1 ...
- 12天学好C语言——记录我的C语言学习之路(Day 10)
12天学好C语言--记录我的C语言学习之路 Day 10: 接着昨天的指针部分学习,有这么一个题目: //还是四个学生,四门成绩,只要有学生一门功课没及格就输出这个学生的所有成绩 /*//progra ...
- angularJs中ui-router的使用
学习使用angular中,ui-route是其中的一个难点,简单使用没什么问题,但涉及到多级嵌套,就感觉有茫然,查了很多资料,踩过很多坑,到目前为止也不能说对ui-route有全面了解:这里只是把填补 ...
- TextView实现跑马灯效果
网上有很多跑马灯的介绍,有很多跑马灯的代码.或许我的不是最好的,但是应该很容易明白的. 我们先来介绍一个跑马灯的代码 <LinearLayout xmlns:android="http ...
- OpenJudge 2756 二叉树
1.链接地址: http://bailian.openjudge.cn/practice/2756/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 如 上图所示,由正整数1 ...
- JSTL之迭代标签库
JSTL之迭代标签库 JSTL的全称是 Java Server Pages Standard Tag Library,翻译过来就是JSP标准标签库,它包含了在开发JSP页面时经常用到的一组标准标签.这 ...