bzoj1823 JSOI2010 满汉全席 2-SAT 经典建模

42 篇文章 0 订阅
31 篇文章 0 订阅

这个题其实我一开始看完没有想到2-SAT

而思路巧妙,编程复杂度低也是我选这个题的原因,在3h4t的赛制中非常适合(好像是病句。。)


具体的来说就是,因为每种材料都有两个选择,那就是做成满式或者是汉式


那么我们可以考虑,在信息学奥赛中,只有两种选择的有哪些。。

看来只有二分图和2-SAT了?


然后我们注意到,每个选择是互相排斥的,那么这不正好符合2-SAT的性质么?——每个布尔变量只能是成立或者是不成立,这与满式汉式这好一样。


好那如果你已经想到2-SAT了以后这题基本就出来了

因为本题根本不用求方案,所以本来准备的专门用于解决2-SAT问题的方法就根本不用了

我们只需要考虑对于每对菜,如果这对中的一个菜被选择了之后另一个菜必须要被选择那么我们就在他们之间连边,具体的连边方法大家自己想吧,或者可以看代码中的MakeGraph函数


那么最后怎么办呢?只需要对原图用tarjan缩点,然后对于每种材料,如果满式和汉式在同一个scc中那么就BAD

否则GOOD


个人感觉这个题还是不错的,符合省选风格

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
#include <algorithm>
#define MAX 2009
#define rep(i, j, k) for(int i = j; i <= k; i++)

using namespace std;

int next[2 * MAX], head[MAX], tot = 0, n, m, to[2 * MAX];
int scc[MAX], dfn[MAX], in[MAX], low[MAX], SccNum = 0;
int DfsClock = 0, Case;

stack<int>s;

inline int calc (char ch, int x)
{
	return 2 * x + (ch != 'm');
}

inline void add (int x, int y)
{
	to[++tot] = y;
	next[tot] = head[x];
	head[x] = tot;
}

inline void MakeGraph()
{
	char ch1, ch2;
	int num1, num2;
	rep (i, 1, m)
	{
		getchar();
		scanf ("%c%d %c%d", &ch1, &num1, &ch2, &num2);
		int point1 = calc (ch1, num1), point2 = calc (ch2, num2);
		add (point1 ^ 1, point2);
		add (point2 ^ 1, point1);
	}
}

inline void init()
{
	scanf ("%d%d", &n, &m);
	SccNum = DfsClock = tot = 0;
	while (!s.empty())
		s.pop();
	memset (in, 0, sizeof (in));
	memset (head, 0, sizeof (head));
	memset (scc, 0, sizeof (scc));
	memset (low, 0, sizeof (low));
	memset (next, 0, sizeof (next));
	memset (dfn, 0, sizeof (dfn));
	MakeGraph();
}

inline void tarjan (int x)
{
	dfn[x] = low[x] = ++DfsClock;
	s.push (x);
	in[x] = 1;
	for (int i =head[x]; i; i = next[i])
		if (!dfn[to[i]])
		{
			tarjan (to[i]);
			low[x] = min (low[x], low[to[i]]);
		}
		else
			if (in[to[i]])
				low[x] = min (low[x], dfn[to[i]]);
	if (low[x] == dfn[x])
	{
		SccNum++;
		while (1)
		{
			int now = s.top();
			scc[now] = SccNum;
			s.pop();
			in[now] = 0;
			if (now == x)
				break;
		}
	}
	return;
}

inline void work()
{
	rep (i, 2, 2 * n + 1)
		if (!dfn[i])
			tarjan (i);
}

inline void print()
{
	for (int i = 2; i <= 2 * n; i += 2)
		if (scc[i] == scc[i ^ 1])
		{
			printf ("BAD\n");
			return;
		}
	printf ("GOOD\n");
	return;
}

int main()
{
	scanf ("%d",&Case);
	while (Case--)
	{
		init();
		work();
		print();
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值