【POJ】2942 Knights of the Round Table 点双连通经典题
来源:互联网 发布:淘宝怎么看买家来源 编辑:程序博客网 时间:2024/05/21 07:03
Knights of the Round Table
Time Limit: 7000MS
Memory Limit: 65536KTotal Submissions: 9411
Accepted: 3045
Memory Limit: 65536KTotal Submissions: 9411
Accepted: 3045
Description
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country.
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
- The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
- An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ``yes" and ``no" have the same number of votes, and the argument goes on.)
Input
The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ).
The input is terminated by a block with n = m = 0 .
The input is terminated by a block with n = m = 0 .
Output
For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled.
Sample Input
5 51 41 52 53 44 50 0
Sample Output
2
Hint
Huge input file, 'scanf' recommended to avoid TLE.
Source
Central Europe 2005
传送门:【POJ】2942 Knights of the Round Table
题目大意:
有n个骑士经常举行圆桌会议,每次圆桌会议至少应该有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置。如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防赞同和反对票一样多。现在你知道哪些骑士相互憎恨后,你的任务是统计有多少个骑士不可能参加任何一个会议。
题目分析:假设不能相邻的骑士之间有一条边,以此构成图G,那么能够相邻的骑士之间建边构成G的补图G',则题目转化成求不在任何一个简单奇圈上的节点数。如果图G不连通,应该对每个连通分量求解。
简单圈上的点必定属于同一个双连通分量,因此需要找到所有的双连通分量。因为二分图是没有奇圈的(因为总是能够做到黑白染色),所以我们只需要找不是二分图的双连通分量。怎么找?黑白染色判定,如果一个连通分量可以被黑白染色(即相邻两点颜色不同),则说明该双连通分量是一个二分图,否则该双连通分量不是二分图(必定包含奇圈)。
我们只需要在每得到一个连通块时就进行染色判定即可。
代码如下:
传送门:【POJ】2942 Knights of the Round Table
题目大意:
有n个骑士经常举行圆桌会议,每次圆桌会议至少应该有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置。如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防赞同和反对票一样多。现在你知道哪些骑士相互憎恨后,你的任务是统计有多少个骑士不可能参加任何一个会议。
题目分析:假设不能相邻的骑士之间有一条边,以此构成图G,那么能够相邻的骑士之间建边构成G的补图G',则题目转化成求不在任何一个简单奇圈上的节点数。如果图G不连通,应该对每个连通分量求解。
简单圈上的点必定属于同一个双连通分量,因此需要找到所有的双连通分量。因为二分图是没有奇圈的(因为总是能够做到黑白染色),所以我们只需要找不是二分图的双连通分量。怎么找?黑白染色判定,如果一个连通分量可以被黑白染色(即相邻两点颜色不同),则说明该双连通分量是一个二分图,否则该双连通分量不是二分图(必定包含奇圈)。
我们只需要在每得到一个连通块时就进行染色判定即可。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define up( i ) ( ( i ) << 1 )#define down( i ) ( ( i ) << 1 | 1 )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i )#define clear( a , x ) memset ( a , x , sizeof a )const int MAXN = 1005 ;const int MAXE = 1000005 ;const int INF = 0x7f7f7f7f ;struct Edge {int v , n ;Edge ( int var = 0 , int next = 0 ) :v ( var ) , n ( next ) {}} ;struct BCC {Edge edge[MAXE] ;int adj[MAXN] , cntE ;int bcc[MAXN] , bcc_cnt ;int low[MAXN] , dfn[MAXN] , dfs_clock ;int S[MAXN] , top ;int n , m ;int color[MAXN] ;bool G[MAXN][MAXN] ;bool odd[MAXN] ;void init () {cntE = top = bcc_cnt = dfs_clock = 0 ;clear ( G , 0 ) ;clear ( bcc , 0 ) ;clear ( odd , 0 ) ;clear ( dfn , 0 ) ;clear ( adj , -1 ) ;}void addedge ( int u , int v ) {edge[cntE] = Edge ( v , adj[u] ) ;adj[u] = cntE ++ ;edge[cntE] = Edge ( u , adj[v] ) ;adj[v] = cntE ++ ;}int draw ( int u , int co ) {for ( int i = adj[u] ; ~i ; i = edge[i].n ) {int v = edge[i].v ;if ( bcc[v] != co )continue ;if ( !color[v] ) {color[v] = 3 - color[u] ;if ( !draw ( v , co ) )return 0 ;}else if ( color[v] != 3 - color[u] )return 0 ;}return 1 ;}void tarjan ( int u , int fa ) {low[u] = dfn[u] = ++ dfs_clock ;S[top ++] = u ;int flag = 1 ;for ( int i = adj[u] ; ~i ; i = edge[i].n ) {int v = edge[i].v ;if ( v == fa && flag ) {flag = 0 ;continue ;}if ( !dfn[v] ) {tarjan ( v , u ) ;low[u] = min ( low[u] , low[v] ) ;if ( low[v] >= dfn[u] ) {++ bcc_cnt ;while ( 1 ) {int x = S[-- top] ;bcc[x] = bcc_cnt ;if ( x == v )break ;}bcc[u] = bcc_cnt ;clear ( color , 0 ) ;color[u] = 1 ;if ( !draw ( u , bcc_cnt ) )REPF ( j , 1 , n )if ( bcc[j] == bcc_cnt )odd[j] = 1 ;}}elselow[u] = min ( low[u] , dfn[v] ) ;}}void input () {int u , v ;REP ( i , m ) {scanf ( "%d%d" , &u , &v ) ;G[u][v] = G[v][u] = 1 ;}REPF ( i , 1 , n )REPF ( j , i + 1 , n )if ( !G[i][j] )addedge ( i , j ) ;}void solve () {int cnt = 0 ;REPF ( i , 1 , n )if ( !dfn[i] )tarjan ( i , -1 ) ;REPF ( i , 1 , n )if ( !odd[i] )++ cnt ;printf ( "%d\n" , cnt ) ;}} ;BCC z ;void work () {int n , m ;while ( ~scanf ( "%d%d" , &z.n , &z.m ) && ( z.n || z.m ) ) {z.init () ;z.input () ;z.solve () ;}}int main () {work () ;return 0 ;}
0 0
- 【POJ】2942 Knights of the Round Table 点双连通经典题
- poj 2942 Knights of the Round Table 点双连通
- poj 2942 Knights of the Round Table 点-双连通分量 图论综合题
- poj 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 (点双连通分量求解)
- 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 【双连通缩点+判奇圈】【经典】
- 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 [二分图染色][点双连通分量]
- POJ 2942 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
- [双连通] poj 2942 Knights of the Round Table#双连通+交叉染色
- php下实现折线图效果的代码
- 动态规划4-最优二叉查找树
- Java基础知识——String类笔记
- JAVA内存模型
- hadoop编程小技巧(1)---map端聚合
- 【POJ】2942 Knights of the Round Table 点双连通经典题
- 禅知系统安装手册(sae平台)
- ORA-12541:TNS:无监听程序
- 数显表和PLC中的数值不一致怎么办?
- oracle备忘录(四)--分组函数
- 单例的优点,缺点以及应用场景
- tcp连接和断开流程
- 基本的深搜 (被玩屎了)
- DedeCms安装了【站内新闻】模块后导致的错误(BUG)