예제 #1
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));
        }