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: Wrong Answer)

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

using SyrupBeans.Competitive.IO;

using E = System.Linq.Enumerable;

public class Program
{
    public static void Main()
    {
        var n = InputParser.TakeInt32();
        var witness = new int[n + 1];
        var mention = new int[n + 1];
        for (int i = 1; i < witness.Length; ++i)
        {
            var a = InputParser.TakeInt32();
            for (int j = 0; j < a; ++j)
            {
                var xy = InputParser.TakeInt32Array(2);
                witness[i] |= xy[1] << xy[0] - 1;
                mention[i] |= 1 << xy[0] - 1;
            }
        }

        var max = 0;
        var upperBound = 1 << n;
        for (int i = 0; i < upperBound; ++i)
        {
            var k = 0;
            var ok = true;
            for (int j = 0; j < n && ok; ++j)
            {
                var w = ~witness[j + 1];
                if ((i >> j & 1) != 0)
                {
                    w = ~w;
                    ++k;
                }
                ok &= (w & mention[j + 1]) == (i & mention[j + 1]);
            }
            if (!ok)
            {
                continue;
            }
            max = Math.Max(max, k);
        }

        Console.WriteLine(max);
    }
}

namespace SyrupBeans.Competitive.IO
{
    public static class InputParser
    {
        public static string BufferdString { get; private set; }
        private static int index;
        private static char Peek => BufferdString[index];

        public static char TakeCharacter() => BufferdString[index++];

        static InputParser()
        {
            Reset();
        }

        public static void Reset()
        {
            InputParser.BufferdString = System.Console.In.ReadToEnd();
            InputParser.index = 0;
        }

        private static void SeekToken()
        {
            while (char.IsWhiteSpace(InputParser.Peek))
            {
                ++InputParser.index;
            }
        }

        private static char[] Separators { get; } = " \r\n".ToCharArray();

        private static int CountTokens(string terminator)
        {
            int k = 0;
            int i = InputParser.index;
            int t = InputParser.BufferdString.IndexOf(terminator, i);
            if (t < 0)
            {
                t = InputParser.BufferdString.Length;
            }
            var c = InputParser.Peek;
            while (i < 0 || t <= i)
            {
                while (char.IsWhiteSpace(c))
                {
                    ++i;
                    if (i >= t)
                    {
                        return k;
                    }
                    c = InputParser.BufferdString[i];
                }
                ++k;
                i = InputParser.BufferdString.IndexOfAny(Separators, i);
            }
            return k;
        }

        public static int TakeInt32()
        {
            InputParser.SeekToken();
            int sign = 1;
            if (InputParser.Peek == '-')
            {
                ++InputParser.index;
                sign = -1;
            }
            int x = 0;
            while (char.IsDigit(InputParser.Peek))
            {
                x = x * 10 + InputParser.TakeCharacter() - '0';
            }
            x *= sign;
            return x;
        }

        public static int[] TakeInt32Array(int length)
        {
            var xs = new int[length];
            for (int i = 0; i < xs.Length; ++i)
            {
                xs[i] = InputParser.TakeInt32();
            }
            return xs;
        }

        public static int[] TakeInt32Array(string terminator = "\0")
            => InputParser.TakeInt32Array(InputParser.CountTokens(terminator));
    }
}