結果

問題 No.274 The Wall
ユーザー tktk_snsntktk_snsn
提出日時 2020-11-28 19:19:50
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
MLE  
実行時間 -
コード長 3,735 bytes
コンパイル時間 595 ms
コンパイル使用メモリ 11,132 KB
実行使用メモリ 633,520 KB
最終ジャッジ日時 2023-10-10 00:54:57
合計ジャッジ時間 4,878 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
15,372 KB
testcase_01 AC 17 ms
8,296 KB
testcase_02 AC 17 ms
8,232 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class SCC_graph(object):
    def __init__(self, n):
        """n:ノード数"""
        self.n = n
        self.edges = []

    def add_edge(self, frm, to):
        """frm -> toへ有効辺を張る"""
        self.edges.append((frm, to))

    def __csr(self):
        self.start = [0] * (self.n + 1)
        self.elist = [0] * len(self.edges)
        for frm, to in self.edges:
            self.start[frm + 1] += 1
        for i in range(1, self.n + 1):
            self.start[i] += self.start[i - 1]
        cnt = self.start[:]
        for frm, to in self.edges:
            self.elist[cnt[frm]] = to
            cnt[frm] += 1

    def __dfs(self, v):
        self.low[v] = self.now_ord
        self.order[v] = self.now_ord
        self.now_ord += 1
        self.visited.append(v)
        for i in range(self.start[v], self.start[v + 1]):
            to = self.elist[i]
            if self.order[to] == -1:
                self.__dfs(to)
                self.low[v] = min(self.low[v], self.low[to])
            else:
                self.low[v] = min(self.low[v], self.order[to])
        if self.low[v] == self.order[v]:
            while self.visited:
                u = self.visited.pop()
                self.order[u] = self.n
                self.ids[u] = self.group_num
                if u == v:
                    break
            self.group_num += 1

    def _make_scc_ids(self):
        self.__csr()
        self.now_ord = 0
        self.group_num = 0
        self.visited = []
        self.low = [0] * self.n
        self.ids = [0] * self.n
        self.order = [-1] * self.n
        for i in range(self.n):
            if self.order[i] == -1:
                self.__dfs(i)
        for i in range(self.n):
            self.ids[i] = self.group_num - 1 - self.ids[i]

    def scc(self):
        self._make_scc_ids()
        groups = [[] for _ in range(self.group_num)]
        for i in range(self.n):
            groups[self.ids[i]].append(i)
        return groups


class TwoSAT(SCC_graph):
    def __init__(self, n):
        """ n: ノード数"""
        self._n = n
        super().__init__(2 * n)

    def add_clause(self, i, f, j, g):
        """ (xi == f)∨(xj == g)というクローズを追加 """
        x = 2 * i + (0 if f else 1)
        y = 2 * j + (1 if g else 0)
        self.add_edge(x, y)
        x = 2 * j + (0 if g else 1)
        y = 2 * i + (1 if f else 0)
        self.add_edge(x, y)

    def satisfiable(self):
        """ 条件を満たす割り当てが存在するか判定する """
        self._make_scc_ids()
        self._answer = [False] * self._n
        for i in range(self._n):
            if self.ids[2 * i] == self.ids[2 * i + 1]:
                return False
            self._answer[i] = (self.ids[2 * i] < self.ids[2 * i + 1])
        return True

    def answer(self):
        """ 最後に読んだsatisfiableのクローズを満たす割り当てを返す """
        return self._answer


def intersect(a, b, c, d):
    if b < c or d < a:
        return False
    return True


n, m = map(int, input().split())
LR = tuple(tuple(map(int, input().split())) for _ in range(n))

ts = TwoSAT(n)

for i in range(n - 1):
    a, b = LR[i]
    for j in range(i + 1, n):
        c, d = LR[j]
        if intersect(a, b, c, d):
            ts.add_clause(i, 0, j, 0)
        if intersect(a, b, m - d - 1, m - c - 1):
            ts.add_clause(i, 0, j, 1)
        if intersect(m - b - 1, m - a - 1, c, d):
            ts.add_clause(i, 1, j, 0)
        if intersect(m - b - 1, m - a - 1, m - d - 1, m - c - 1):
            ts.add_clause(i, 1, j, 1)

if ts.satisfiable():
    print("YES")
else:
    print("NO")
0