题目描述
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;
}