HDU 1824 Let's go home (2-SAT判定)

Let‘s go home

Time Limit: 10000/1000 MS (Java/Others)    Memory
Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1616    Accepted Submission(s): 661

Problem Description

小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头。

—— 余光中

集训是辛苦的,道路是坎坷的,休息还是必须的。经过一段时间的训练,lcy决定让大家回家放松一下,但是训练还是得照常进行,lcy想出了如下回家规定,每一个队(三人一队)或者队长留下或者其余两名队员同时留下;每一对队员,如果队员A留下,则队员B必须回家休息下,或者B留下,A回家。由于今年集训队人数突破往年同期最高记录,管理难度相当大,lcy也不知道自己的决定是否可行,所以这个难题就交给你了,呵呵,好处嘛~,免费**漂流一日。

Input

第一行有两个整数,T和M,1<=T<=1000表示队伍数,1<=M<=5000表示对数。

接下来有T行,每行三个整数,表示一个队的队员编号,第一个队员就是该队队长。

然后有M行,每行两个整数,表示一对队员的编号。

每个队员只属于一个队。队员编号从0开始。

Output

可行输出yes,否则输出no,以EOF为结束。

Sample Input

1 2
0 1 2
0 1
1 2

2 4
0 1 2
3 4 5
0 3
0 4
1 3
1 4

Sample Output

yes
no

Author

威士忌

Source

HDOJ 2007 Summer Exercise(3)- Hold by Wiskey

解题思路:

2-SAT就是两者有冲突就连边。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#define LL long long
using namespace std;
const int MAXN = 20010;
vector<int>G[MAXN];
int pre[MAXN], lowlink[MAXN], sccno[MAXN], dfs_clock, scc_cnt;
stack<int> s;
void dfs(int u)
{
	pre[u] = lowlink[u] = ++dfs_clock;
	s.push(u); int sz = G[u].size();
	for(int i=0;i<sz;i++)
	{
		int v = G[u][i];
		if(!pre[v])
		{
			dfs(v);
			lowlink[u] = min(lowlink[u], lowlink[v]);
		}
		else if(!sccno[v])
		{
			lowlink[u] = min(lowlink[u], pre[v]);
		}
	}
	if(lowlink[u] == pre[u])
	{
		scc_cnt++;
		for(;;)
		{
			int x = s.top(); s.pop();
			sccno[x] = scc_cnt;
			if(x == u) break;
		}
	}
}
void find_scc(int n)
{
	dfs_clock = scc_cnt = 0;
	memset(sccno, 0, sizeof(sccno));
	memset(pre, 0, sizeof(pre));
	for(int i=1;i<=n;i++) if(!pre[i])
		dfs(i);
}
int N, M, T;
int main()
{
	while(scanf("%d%d", &T, &M)!=EOF)
	{
		int x, y, z;
		N = 3 * T;
		for(int i=0;i<=2*N;i++) G[i].clear();
		for(int i=1;i<=T;i++)
		{
			scanf("%d%d%d", &x, &y, &z);
			x++; y++; z++;
			G[x+N].push_back(y);
			G[x+N].push_back(z);
			G[y+N].push_back(x);
			G[z+N].push_back(x);
		}
		for(int i=1;i<=M;i++)
		{
			scanf("%d%d", &x, &y);
			x++; y++;
			G[x].push_back(y + N);
			G[y].push_back(x + N);
		}
		find_scc(2 * N);
		int flag = 1;
		for(int i=1;i<=N;i++)
		{
			if(sccno[i] == sccno[i + N])
			{
				flag = 0;
				break;
			}
		}
		if(flag) puts("yes");
		else puts("no");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 1824 Let's go home (2-SAT判定)

时间: 2024-08-25 16:23:44

HDU 1824 Let's go home (2-SAT判定)的相关文章

HDU 1824 Let&amp;#39;s go home (2-SAT判定)

Let's go home Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1616    Accepted Submission(s): 661 Problem Description 小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头. -- 余光中 集训是辛苦的.道路是坎坷的,歇息还是必须的.经过一段时间的训练,lcy决定让大家

HDU 3062 &amp;&amp; HDU 1824 &amp;&amp; POJ 3578 &amp;&amp; BZOJ 1997 2-SAT

一条边<u,v>表示u选那么v一定被选. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int Maxm=21000; 7 const int Maxn=2010; 8 struct EDGE{int to,next;}edge[Maxm]; 9 int T,m

HDU 1824 Let&#39;s go home (2-SAT)

题目地址:HDU 1824 这题可以把每队的两个队员看成一个,这样就是2-sat水题了... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #incl

HDU 1824

好吧,这次估计是明白边的含义了. X\/Y= -X->Y = -Y->X  这就达成了选了-X必须选Y的目的了.对于这道题,必须要明白题目了. 每一个队(三人一队)或者队长留下或者其余两名队员同时留下 : 就可以得到  X\/(Y^Z)=1 而不是互斥的. 以及  X->-Y     -X->Y 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include &l

HDU 1824 Let&#39;s go home

Let's go home Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1435    Accepted Submission(s): 573 Problem Description 小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头.                        —— 余光中 集训是辛苦的,道路是坎坷的,休息还是

(2 sat) hdu 1824

Let's go home Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1523    Accepted Submission(s): 616 Problem Description 小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头.                        —— 余光中 集训是辛苦的,道路是坎坷的,休息还是

HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

Problem Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are given all pairs o

【小结】2-sat

2?sat 小结 2?sat解决的是可满足性问题,并且每个合取范式中的文字个数不多于2个. 形式为: (a∨?b)∧(?c∨?d)∧(?a∨d)? 将所有a∨b改成(?a?b)∧(?b?a) 建边,每个变量a对应两个点a和a+n 如果存在cmp[a]==cmp[a+n]不成立,否则成立.且如果cmp[a]>cmp[a+n],令a=true,否则令a=false即为原式的一组解. #include <cstdio> #include <iostream> #include &l

2-SAT问题

2-SAT问题 现有一个由N个布尔值组成的序列A,给出一些限制关系,比如A[x]AND A[y]=0.A[x] OR A[y] OR A[z]=1等,要确定A[0..N-1]的值,使得其满足所有限制关系.这个称为SAT问题,特别的,若每种限制关系中最多只对两个元素进行限制,则称为2-SAT问题. 由于在2-SAT问题中,最多只对两个元素进行限制,所以可能的限制关系共有11种: A[x] NOT A[x] A[x] AND A[y] A[x] AND NOT A[y] A[x] OR A[y] A