Problem:
Score : 300 points

Problem Statement

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 A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.

How many honest persons can be among those N people at most?

Constraints

All values in input are integers.

1 \leq N \leq 15

0 \leq A_i \leq N - 1

1 \leq x_{ij} \leq N

x_{ij} \neq i

x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2)

y_{ij} = 0, 1

Input

Input is given from Standard Input in the following format:

N
A_1
x_{11} y_{11}
x_{12} y_{12}
:
x_{1A_1} y_{1A_1}
A_2
x_{21} y_{21}
x_{22} y_{22}
:
x_{2A_2} y_{2A_2}
:
A_N
x_{N1} y_{N1}
x_{N2} y_{N2}
:
x_{NA_N} y_{NA_N}

Output

Print the maximum possible number of honest persons among the N people.

Sample Input 1

3
1
2 1
1
1 1
1
2 0

Sample Output 1

2

If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.

Sample Input 2

3
2
2 1
3 0
2
3 1
1 0
2
1 1
2 0

Sample Output 2

0

Assuming that one or more of them are honest immediately leads to a contradiction.

Sample Input 3

2
1
2 0
1
1 0

Sample Output 3

1

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder
{
    class Program
    {
        static int N;
        static List<Evidence> evidences;

        static void Main(string[] args)
        {
            //[summary]C - HonestOrUnkind2
            N = int.Parse(Console.ReadLine());

            //全員の証言を先に取得しておく
            GetEvidences();

            Console.WriteLine(CountHonests());
        }

        static int CountHonests()
        {
            //人を表すインデックス
            var men = Enumerable.Range(0, N).ToList();

            var lib = new Library.Combination();

            for (int i = N; i > 0; i--)
            {
                var combis = lib.AllCombinations(men, i);

                foreach (var c in combis)
                {
                    if (IsRightAssumption(c))
                    {
                        return i;
                    }
                }
            }

            return 0;
        }

        static bool IsRightAssumption(List<int> combi)
        {
            var men = new int[N];

            //引数で受け取ったインデックスの人を正直者と仮定する
            foreach(var c in combi)
            {
                men[c] = 1;
            }

            foreach(var e in evidences)
            {
                //正直者と仮定した人の証言が、"men"の状態と一致しているかチェック
                if (men[e.Speaker] == 1)
                {
                    if (men[e.X] != e.Y)
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        static void GetEvidences()
        {
            evidences = new List<Evidence>();

            //人数分繰り返し
            for (int i = 0; i < N; i++)
            {
                int A = int.Parse(Console.ReadLine());

                //その人の行った証言の回数分繰り返し
                for (int j = 0; j < A; j++)
                {
                    var line = ReadLine();
                    int x = line[0] - 1;
                    int y = line[1];

                    evidences.Add(new Evidence(i, x, y));
                }
            }
        }

        static List<int> ReadLine()
        {
            var line = Console.ReadLine();
            var arr = line.Split(' ');

            return arr.Select(x => int.Parse(x)).ToList();
        }
    }

    struct Evidence
    {
        public int Speaker { set; get; }
        public int X { set; get; }
        public int Y { set; get; }

        public Evidence(int speaker, int x, int y)
        {
            Speaker = speaker;
            X = x;
            Y = y;
        }
    }
}

namespace Library
{
    class Combination
    {
        public IEnumerable<List<T>> AllCombinations<T>(List<T> source, int n,
                                                       bool isAllowDupication = false)
        {
            //[summary]異なる要素から、n個を選ぶ組み合わせを列挙する
            //"isAllowDupication"を"true"にすると、重複を許可する
            //(重複を許可して、nを要素数と同じにすると、順列を列挙する)

            if (n == 1)
            {
                foreach (var s in source)
                {
                    yield return new List<T> { s };
                }

                yield break;
            }
            else
            {
                for (int i = 0; i < source.Count; i++)
                {
                    List<T> clone;

                    if (isAllowDupication)
                    {
                        //重複を許可する場合は、その要素のみを削除
                        clone = new List<T>(source);
                        clone.RemoveAt(i);
                    }
                    else
                    {
                        //重複を許可しない場合は、自分よりも前の要素はすべて削除
                        clone = new List<T>(source.Skip(i + 1));
                    }

                    //再帰処理
                    var items = AllCombinations(clone, n - 1, isAllowDupication);

                    foreach (var item in items)
                    {
                        item.Insert(0, source[i]);
                        yield return item;
                    }
                }
            }
        }
    }
}