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