HDU 3062 Party 2-SAT

Party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5180    Accepted Submission(s): 1681


Problem Description
有n對夫妻被邀請參加一個聚會,因爲場地的問題,每對夫妻中只有1人可以列席。在2n 個人中,某些人之間有着很大的矛盾(當然夫妻之間是沒有矛盾的),有矛盾的2個人是不會同時出現在聚會上的。有沒有可能會有n 個人同時列席?
 

Input
n: 表示有n對夫妻被邀請 (n<= 1000)
m: 表示有m 對矛盾關係 ( m < (n - 1) * (n -1))

在接下來的m行中,每行會有4個數字,分別是 A1,A2,C1,C2 
A1,A2分別表示是夫妻的編號 
C1,C2 表示是妻子還是丈夫 ,0表示妻子 ,1是丈夫
夫妻編號從 0 到 n -1 
 

Output
如果存在一種情況 則輸出YES 
否則輸出 NO 
 

Sample Input
2 1 0 1 1 1
 

Sample Output
YES
 



2-SAT算法。剛剛學。。這道題練手。。。只需要構圖。然後套版就好。。。

資料:http://www.cnblogs.com/silver-bullet/archive/2013/02/18/2915097.html

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



//HDU3062
//****************************************** 
//2-SAT 強連通縮點 
const int MAXN = 2010; 
const int MAXM = 4000010; 
struct Edge 
{ 
 	int to,next; 
}edge[MAXM]; 
int head[MAXN],tot; 
void init() 
{ 
	tot = 0; 
 	memset(head,-1,sizeof(head)); 
} 
void addedge(int u,int v) 
{ 
	edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; 
} 
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong數組的值1~scc 
int Index,top; 
int scc; 
bool Instack[MAXN]; 
int num[MAXN]; 
void Tarjan(int u) 
{ 
	int v; 
 	Low[u] = DFN[u] = ++Index; 
 	Stack[top++] = u; 
 	Instack[u] = true; 
 	for(int i = head[u];i != -1;i = edge[i].next) 
 	{ 
 		v = edge[i].to; 
 		if( !DFN[v] ) 
 		{ 
 			Tarjan(v); 
 			if(Low[u] > Low[v])Low[u] = Low[v]; 
 		} 
 		else if(Instack[v] && Low[u] > DFN[v]) 
 			Low[u] = DFN[v]; 
 	} 
 	if(Low[u] == DFN[u]) 
 	{ 
	 	scc++; 
	 	do 
	 	{ 
	 		v = Stack[--top]; 
	 		Instack[v] = false; 
	 		Belong[v] = scc; 
	 		num[scc]++; 
	 	} 
	 	while(v != u); 
 	} 
} 

bool solvable(int n)//n是總個數,需要選擇一半 
{ 
	memset(DFN,0,sizeof(DFN)); 
 	memset(Instack,false,sizeof(Instack)); 
 	memset(num,0,sizeof(num)); 
 	Index = scc = top = 0; 
 	for(int i = 0;i < n;i++) 
 		if(!DFN[i]) 
 			Tarjan(i); 
	//for(int i = 0;i < n;i += 2) printf("belong[%d] = %d\n belong[%d] = %d\n",i,Belong[i],i^1,Belong[i^1]);
	for(int i = 0;i < n;i += 2) 
 	{ 
 		if(Belong[i] == Belong[i^1]) 
 		return false; 
 	} 
 	return true; 
} 
//************************************************* 
 
//拓撲排序求任意一組解部分 
/*
queue<int>q1,q2; 
vector<vector<int> > dag;//縮點後的逆向DAG圖 
char color[MAXN];//染色,爲'R'是選擇的 
int indeg[MAXN];//入度 
int cf[MAXN]; 
void solve(int n) 
{ 
	dag.assign(scc+1,vector<int>()); 
 	memset(indeg,0,sizeof(indeg)); 
 	memset(color,0,sizeof(color)); 
 	for(int u = 0;u < n;u++) 
 	for(int i = head[u];i != -1;i = edge[i].next) 
 	{ 
 		int v = edge[i].to; 
 		if(Belong[u] != Belong[v]) 
 		{ 
 			dag[Belong[v]].push_back(Belong[u]); 
 			indeg[Belong[u]]++; 
 		} 
 	} 
 	for(int i = 0;i < n;i += 2) 
 	{ 
 		cf[Belong[i]] = Belong[i^1]; 
 		cf[Belong[i^1]] = Belong[i]; 
 	} 
 	while(!q1.empty())q1.pop(); 
 	while(!q2.empty())q2.pop(); 
 	for(int i = 1;i <= scc;i++) 
 		if(indeg[i] == 0) 
 	q1.push(i); 
 	while(!q1.empty()) 
 	{ 
 		int u = q1.front(); 
 		q1.pop(); 
		if(color[u] == 0) 
 		{ 
 			color[u] = 'R'; 
 			color[cf[u]] = 'B'; 
 		} 
 		int sz = dag[u].size(); 
 		for(int i = 0;i < sz;i++) 
 		{ 
 			indeg[dag[u][i]]--;
 			if(indeg[dag[u][i]] == 0) 
 			q1.push(dag[u][i]); 
 		} 
 	} 
} 
 */

int main() 
{ 
	int n,m; 
	int a1,a2,c1,c2;
	while(~scanf("%d%d",&n,&m)){	
		init();
		for(int i=0;i<m;i++){
			scanf("%d%d%d%d",&a1,&a2,&c1,&c2);
		
			a1*=2;
			a2*=2;
			 if(c1==0&&c2==0)    //如果兩個妻子鬧矛盾。那麼就應該是一個a對的丈夫和b對的妻子或者是a對的妻子和b對的丈夫
	        {
	            addedge(a1,a2+1);
	            addedge(a2,a1+1);
	        } 
	        else if(c1==0&&c2==1)    //如果是a隊的妻子和b對的丈夫鬧矛盾,那麼就因該是a對的妻子和b對的妻子或者a隊的丈夫和b隊的丈夫
	        {
	            addedge(a1,a2);
	            addedge(a2+1,a1+1);
	        }
	        else if(c1==1&&c2==0) //如果是a隊的qi和b隊的妻子鬧矛盾,那麼就應該是a對的丈夫和b隊的丈夫或者a對的妻子和b隊的妻子 
	        {
	            addedge(a1+1,a2+1);
	            addedge(a2,a1);
	        } 
	        else if(c1==1&&c2==1)    //如果是a對丈夫和b隊丈夫鬧矛盾,那麼就應該是a對的妻子和b對的丈夫或者a隊的丈夫和b隊的妻子 
	        {
	            addedge(a1+1,a2);
	            addedge(a2+1,a1);
	        }
	
			/*
			addedge(2*a1+c1,((2*a2+c2)^1));
		 	addedge(2*a2+c2,((2*a1+c1)^1));*/
		}
		if(solvable(n*2)){
			printf("YES\n");
		}
		else printf("NO\n");
	}
	return 0; 
} 


發佈了80 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章