# [BZOJ1823]-[JSOI2010]满汉全席-2-SAT

## 题目

BZOJ1823传送门

### 解法

2-SAT简单题，YY一下就可以做出来

### 下面是自带大长度的代码

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

short T , N , M , oppo[205] , tp , head[205] ;
struct Path{
short pre , to ;
}p[2*1000+5] ;

template <typename T>
T min( T x , T y ){
return x < y ? x : y ;
}

char getcha(){
char rt = getchar() ;
while( rt < 'a' || rt > 'z' ) rt = getchar() ;
return rt ;
}

void In( short t1 , short t2 ){
p[ head[t1] = tp ].to = t2 ;
}

short sta[205] , topp ;
short dfn[205] , dfs_c , scc[205] , scc_cnt ;
short dfs( int u ){
sta[++topp] = u ;
short lowu = dfn[u] = ++dfs_c ;
for( short i = head[u] ; i ; i = p[i].pre ){
short v = p[i].to ;
if( !dfn[v] ) lowu = min( lowu , dfs( v ) ) ;
else if( !scc[v] ) lowu = min( lowu , dfn[v] ) ;
}
if( dfn[u] == lowu ){
scc_cnt ++ ;
while( 1 ){
int x = sta[topp--] ;
scc[x] = scc_cnt ;
if( x == u ) break ;
}
} return lowu ;
}

void solve(){
for( short i = 1 ; i <= ( N<<1 ) ; i ++ )
if( !dfn[i] ) dfs( i ) ;
for( short i = 1 ; i <= N ; i ++ ){
if( scc[i] == scc[ oppo[i] ] ){ puts( "BAD" ) ; return ; }
} puts( "GOOD" ) ;
}

void clear(){
tp = dfs_c = scc_cnt = 0 ;
memset( dfn , 0 , sizeof( dfn ) ) ;
memset( scc , 0 , sizeof( scc ) ) ;
}

int main(){
scanf( "%hd" , &T ) ;
char c1 , c2 ;
while( T -- ){
clear() ;
scanf( "%hd%hd" , &N , &M ) ;
for( short i = 1 ; i <= N ; i ++ )
oppo[i] = i + N , oppo[i+N] = i ;
for( short i = 1 , id1 , id2 ; i <= M ; i ++ ){
c1 = getcha() ; scanf( "%hd" , &id1 ) ;
c2 = getcha() ; scanf( "%hd" , &id2 ) ;
// h is origin, and m is opposite
if( c1 == 'h' && c2 == 'm' ){
In( id2 , id1 ) , In( oppo[id1] , oppo[id2] ) ;
} else if( c1 == 'h' && c2 == 'h' ){
In( oppo[id1] , id2 ) , In( oppo[id2] , id1 ) ;
} else if( c1 == 'm' && c2 == 'h' ){
In( id1 , id2 ) , In( oppo[id2] , oppo[id1] ) ;
} else
In( id1 , oppo[id2] ) , In( id2 , oppo[id1] ) ;
}
solve() ;
}
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120