public void InitializeTest()
 {
     Assert.DoesNotThrow(() => _ = new TwoSatisfiability(0));
     Assert.DoesNotThrow(() => _ = new TwoSatisfiability(2));
     Assert.Throws <ArgumentOutOfRangeException>(() => _ = new TwoSatisfiability(-1));
     Assert.That(new StronglyConnectedComponent(0).Length, Is.Zero);
     Assert.That(new StronglyConnectedComponent(2).Length, Is.EqualTo(2));
 }
        public void StressTest()
        {
            for (var ph = 0; ph < 10000; ph++)
            {
                var n        = Utilities.RandomInteger(1, 20);
                var m        = Utilities.RandomInteger(1, 100);
                var expected = new bool[n].Select(_ => Utilities.RandomBool()).ToArray();
                var ts       = new TwoSatisfiability(n);
                var xs       = new int[m];
                var ys       = new int[m];
                var types    = new int[m];
                for (var i = 0; i < m; i++)
                {
                    var x    = Utilities.RandomInteger(0, n - 1);
                    var y    = Utilities.RandomInteger(0, n - 1);
                    var type = Utilities.RandomInteger(0, 2);
                    xs[i]    = x;
                    ys[i]    = y;
                    types[i] = type;
                    switch (type)
                    {
                    case 0:
                        ts.AddClause(x, expected[x], y, expected[y]);
                        break;

                    case 1:
                        ts.AddClause(x, !expected[x], y, expected[y]);
                        break;

                    default:
                        ts.AddClause(x, expected[x], y, !expected[y]);
                        break;
                    }
                }

                Assert.That(ts.IsSatisfiable, Is.True);
                var actual = ts.Answer;
                for (var i = 0; i < m; i++)
                {
                    var(x, y, type) = (xs[i], ys[i], types[i]);
                    switch (type)
                    {
                    case 0:
                        Assert.That(actual[x] == expected[x] || actual[y] == expected[y], Is.True);
                        break;

                    case 1:
                        Assert.That(actual[x] != expected[x] || actual[y] == expected[y], Is.True);
                        break;

                    default:
                        Assert.That(actual[x] == expected[x] || actual[y] != expected[y], Is.True);
                        break;
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static void Solve()
        {
            var ND = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();

            var(N, D) = (ND[0], ND[1]);
            var X = new int[N];
            var Y = new int[N];

            for (var i = 0; i < N; i++)
            {
                var XY = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
                (X[i], Y[i]) = (XY[0], XY[1]);
            }

            var ts = new TwoSatisfiability(N);

            for (var i = 0; i < N; i++)
            {
                for (var j = i + 1; j < N; j++)
                {
                    if (Math.Abs(X[i] - X[j]) < D)
                    {
                        ts.AddClause(i, false, j, false);
                    }
                    if (Math.Abs(X[i] - Y[j]) < D)
                    {
                        ts.AddClause(i, false, j, true);
                    }
                    if (Math.Abs(Y[i] - X[j]) < D)
                    {
                        ts.AddClause(i, true, j, false);
                    }
                    if (Math.Abs(Y[i] - Y[j]) < D)
                    {
                        ts.AddClause(i, true, j, true);
                    }
                }
            }

            if (!ts.IsSatisfiable())
            {
                Console.WriteLine("No");
                return;
            }

            Console.WriteLine("Yes");
            var answer = ts.Answer;

            for (var i = 0; i < N; i++)
            {
                Console.WriteLine(answer[i] ? X[i] : Y[i]);
            }
        }
        public void OneTest()
        {
            var ts = new TwoSatisfiability(1);

            ts.AddClause(0, true, 0, true);
            ts.AddClause(0, false, 0, false);
            Assert.That(ts.IsSatisfiable(), Is.False);

            ts = new TwoSatisfiability(1);
            ts.AddClause(0, true, 0, true);
            Assert.That(ts.IsSatisfiable(), Is.True);
            Assert.That(new[] { true }, Is.EqualTo(ts.Answer));

            ts = new TwoSatisfiability(1);
            ts.AddClause(0, false, 0, false);
            Assert.That(ts.IsSatisfiable(), Is.True);
            Assert.That(new[] { false }, Is.EqualTo(ts.Answer));
        }
        public void EmptyTest()
        {
            var ts = new TwoSatisfiability(0);

            Assert.That(new bool[] { }, Is.EqualTo(ts.Answer));
        }
        public void InvalidArgumentsTest(int i, int j)
        {
            var ts = new TwoSatisfiability(2);

            Assert.Throws <ArgumentOutOfRangeException>(() => ts.AddClause(i, true, j, true));
        }