问题 I: HonestOrUnkind2----------------------------思维(二进制枚举)

53 篇文章 0 订阅
4 篇文章 0 订阅

题目描述
There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.
Person i gives Ai testimonies. The j-th testimony by Person i is represented by two integers xij and yij. If yij=1, the testimony says Person xij is honest; if yij=0, it says Person xij is unkind.
How many honest persons can be among those N people at most?

Constraints
All values in input are integers.
1≤N≤15
0≤Ai≤N−1
1≤xij≤N
xij≠i
xij1≠xij2(j1≠j2)
yij=0,1

输入

在这里插入图片描述
输出
Print the maximum possible number of honest persons among the N people.

题意:
一共有n个人,诚实的人一直说真话,不诚实的人要么说真话要么说假话
对于每个人都有几组信息x,y 表示 第i个人说 x 是诚实的人(y=1) 要么就是第i个人说 x是不诚实的人(y=0)
解析:
我们用二进制枚举,0表示不诚实 1表示诚实。那么最多只会有(1<<15) 这些状态。
我们只要枚举那些说真话的人
判断两个条件(互相矛盾的)
第一个:第i个人说x 诚实,但是在当前的二进制状态下它是0,就矛盾了
第二个:第i个人说x 不诚实,但是在当前的二进制状态下它是1,就矛盾了。

就是下面这两个判断条件

if(b&&!(tmp&(1<<(a-1)))) return 0;
if(!b&&(tmp&(1<<(a-1)))) return 0;
#include<bits/stdc++.h>
using namespace std;
const int N=15;
#define x first
#define y second
vector<pair<int,int> > v[20];
int n,m,a,b;
bool check(int tmp)
{
	for(int i=1;i<=n;i++)
	{
		if(tmp&(1<<(i-1)))  //当前第i个人说了真话
		{
			int m=v[i].size();
			for(int j=0;j<m;j++)//就遍历第i个人的那个分组所有人
			{
				int a=v[i][j].first;
				int b=v[i][j].second;
				if(b&&!(tmp&(1<<(a-1)))) return 0;
				if(!b&&(tmp&(1<<(a-1)))) return 0;
			}
		}
	}
	return 1;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>m;
		for(int j=1;j<=m;j++ )
		{
			cin>>a>>b;
			v[i].push_back({a,b});
		}
	}
	int maxn=0;
	for(int i=1;i<(1<<n);i++)
	{
		if(check(i))
		{
			int ans=0;
			for(int j=1;j<=n;j++)//只要加一下那些诚实的人(就是1)
			  ans=ans+(i>>(j-1)&1);
			maxn=max(maxn,ans);
		}
	}
	cout<<maxn<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值