【POJ】3678 Katu Puzzle 2-sat

传送门:【POJ】3678 Katu Puzzle


题目分析:

设对于布尔变量 x ,x << 1表示 x 取 1,x << 1 | 1 表示 x 取 0 。

如果必须取 x = 1 则建边<~x,x>,必须取 x = 0 则建边<x,~x>。

每种操作建图如下:

ADD c = 1:< ~u , u > , < ~v , v >(u,v必须取1)

ADD c = 0:< u , ~v > , < v , ~u >(u,v至少一个取0)

OR   c = 1:< ~u , v > , < ~v , u >(u,v至少一个取1)

OR   c = 0:< u , ~u > , < v , ~v >(u,v必须取0)

XOR c = 1:< u , ~v > , < v , ~u > , < ~u , v > , < ~v , u >(u,v取值必须不同)

XOR c = 0:< u , v > , < ~v , ~u > , < ~u , ~v > , < v , u >(u,v取值必须相同)

最后跑一遍强连通判断就OK了。


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define REV( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )

const int MAXN = 2005 ;
const int MAXE = 5000000 ;

struct Edge {
	int v ;
	Edge* next ;
} ;

Edge E[MAXE] , *H[MAXN] , *cur ;
int dfn[MAXN] , low[MAXN] , scc[MAXN] , scc_cnt ;
int S[MAXN] , top , dfs_clock ;
int n , m ;

void init () {
	cur = E ;
	top = 0 ;
	scc_cnt = 0 ;
	dfs_clock = 0 ;
	CLR ( H , 0 ) ;
	CLR ( dfn , 0 ) ;
	CLR ( scc , 0 ) ;
}

void addedge ( int u , int v ) {
	cur -> v = v ;
	cur -> next = H[u] ;
	H[u] = cur ++ ;
}

void tarjan ( int u ) {
	dfn[u] = low[u] = ++ dfs_clock ;
	S[top ++] = u ;
	for ( Edge* e = H[u] ; e ; e = e -> next ) {
		int v = e -> v ;
		if ( !dfn[v] ) {
			tarjan ( v ) ;
			low[u] = min ( low[u] , low[v] ) ;
		} else if ( !scc[v] ) low[u] = min ( low[u] , dfn[v] ) ;
	}
	if ( low[u] == dfn[u] ) {
		++ scc_cnt ;
		do {
			scc[S[-- top]] = scc_cnt ;
		} while ( u != S[top] ) ;
	}
}

void scanf ( int& x , char c = 0 ) {
	while ( ( c = getchar () ) < '0' || c > '9' ) ;
	x = c - '0' ;
	while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
}

int solve () {
	int u , v , c ;
	char s[5] ;
	init () ;
	while ( m -- ) {
		scanf ( u ) , scanf ( v ) , scanf ( c ) , scanf ( "%s" , s ) ;
		if ( s[0] == 'A' ) {
			if ( c == 1 ) {
				addedge ( u << 1 | 1 , u << 1 ) ;
				addedge ( v << 1 | 1 , v << 1 ) ;
			} else {
				addedge ( u << 1 , v << 1 | 1 ) ;
				addedge ( v << 1 , u << 1 | 1 ) ;
			}
		} else if ( s[0] == 'O' ) {
			if ( c == 1 ) {
				addedge ( u << 1 | 1 , v << 1 ) ;
				addedge ( v << 1 | 1 , u << 1 ) ;
			} else {
				addedge ( u << 1 , u << 1 | 1 ) ;
				addedge ( v << 1 , v << 1 | 1 ) ;
			}
		} else {
			if ( c == 1 ) {
				addedge ( u << 1 , v << 1 | 1 ) ;
				addedge ( v << 1 , u << 1 | 1 ) ;
				addedge ( u << 1 | 1 , v << 1 ) ;
				addedge ( v << 1 | 1 , u << 1 ) ;
			} else {
				addedge ( u << 1 , v << 1 ) ;
				addedge ( v << 1 , u << 1 ) ;
				addedge ( u << 1 | 1 , v << 1 | 1 ) ;
				addedge ( v << 1 | 1 , u << 1 | 1 ) ;
			}
		}
	}
	REP ( i , 0 , n << 1 ) if ( !dfn[i] ) tarjan ( i ) ;
	REP ( i , 0 , n ) if ( scc[i << 1] == scc[i << 1 | 1] ) return 0 ;
	return 1 ;
}

int main () {
	while ( ~scanf ( "%d%d" , &n , &m ) ) printf ( solve () ? "YES\n" : "NO\n" ) ;
	return 0 ;
}


你可能感兴趣的:(poj)