結果

問題 No.274 The Wall
ユーザー ygd.ygd.
提出日時 2022-01-13 23:12:59
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 4,999 bytes
コンパイル時間 1,184 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 841,364 KB
最終ジャッジ日時 2023-08-11 04:34:04
合計ジャッジ時間 7,142 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,420 KB
testcase_01 AC 72 ms
71,284 KB
testcase_02 AC 75 ms
71,192 KB
testcase_03 AC 1,428 ms
488,572 KB
testcase_04 AC 214 ms
71,348 KB
testcase_05 AC 71 ms
70,884 KB
testcase_06 AC 71 ms
71,260 KB
testcase_07 AC 71 ms
71,272 KB
testcase_08 AC 74 ms
71,060 KB
testcase_09 AC 71 ms
71,208 KB
testcase_10 AC 70 ms
71,248 KB
testcase_11 MLE -
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.readline #文字列につけてはダメ
#input = sys.stdin.buffer.readline #文字列につけてはダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#MOD = pow(10,9) + 7
#MOD = 998244353

#https://atcoder.jp/contests/practice2/submissions/17674275
class scc_graph:
 
    def __init__(self, N):
        self.N = N
        self.edges = []
 
    def csr(self):
        start = [0]*(self.N+1)
        elist = [0]*len(self.edges)
        for v, w in self.edges:
            start[v+1] += 1
        for i in range(1, self.N+1):
            start[i] += start[i-1]
        counter = start.copy()
        for v, w in self.edges:
            elist[counter[v]] = w
            counter[v] += 1
        self.start = start
        self.elist = elist
 
    def add_edge(self, v, w):
        self.edges.append((v, w))
 
    def scc_ids(self):
        self.csr()
        N = self.N
        now_ord = group_num = 0
        visited = []
        low = [0]*N
        order = [-1]*N
        ids = [0]*N
        parent = [-1]*N
        stack = []
        for i in range(N):
            if order[i] == -1:
                stack.append(~i)
                stack.append(i)
                while stack:
                    v = stack.pop()
                    if v >= 0:
                        if order[v] == -1:
                            low[v] = order[v] = now_ord
                            now_ord += 1
                            visited.append(v)
                            for i in range(self.start[v], self.start[v+1]):
                                to = self.elist[i]
                                if order[to] == -1:
                                    stack.append(~to)
                                    stack.append(to)
                                    parent[to] = v
                                else:
                                    low[v] = min(low[v], order[to])
                    else:
                        v = ~v
                        if low[v] == order[v]:
                            while True:
                                u = visited.pop()
                                order[u] = N
                                ids[u] = group_num
                                if u == v:
                                    break
                            group_num += 1
                        if parent[v] != -1:
                            low[parent[v]] = min(low[parent[v]], low[v])
        for i, x in enumerate(ids):
            ids[i] = group_num-1-x
 
        return group_num, ids
 
    def scc(self):
        group_num, ids = self.scc_ids()
        groups = [[] for _ in range(group_num)]
        for i, x in enumerate(ids):
            groups[x].append(i)
        return groups
 
 
class two_sat:
 
    def __init__(self, N):
        self.N = N
        self.answer = []
        self.scc = scc_graph(2*N)
 
    def add_clause(self, i, f, j, g): #f,g = 0/1 (True/False)
        # assert 0 <= i < self.N
        # assert 0 <= j < self.N
        self.scc.add_edge(2*i+(f == 0), 2*j+(g == 1))
        self.scc.add_edge(2*j+(g == 0), 2*i+(f == 1))
 
    def satisfiable(self): #これがTrueならts.answerで満たす場合の組み合わせを出力可能
        _, ids = self.scc.scc_ids()
        self.answer.clear()
        for i in range(self.N):
            if ids[2 * i] == ids[2 * i + 1]:
                self.answer.clear()
                return False
            self.answer.append(ids[2*i] < ids[2*i+1])
        return True
 
def overlap(a,b,c,d): #Trueは被る
    if b < c: return False
    if d < a: return False
    return True


def main():
    N,M = map(int,input().split())
    L = []; R = []
    for i in range(N):
        l,r = map(int,input().split()) #0-index
        L.append(l)
        R.append(r)

    ts = two_sat(N)
    for i in range(N): #全てのペアをチェックしてOverlapするところが一か所でもあったらダメ
        for j in range(i+1,N):
            li0, ri0 = L[i], R[i]
            lj0, rj0 = L[j], R[j]
            li1, ri1 = M-1 - ri0, M-1 - li0
            lj1, rj1 = M-1 - rj0, M-1 - lj0
            #print(li0, ri0, li1, ri1)
            #print(lj0, rj0, lj1, rj1)
            if overlap(li0, ri0, lj0, rj0): #0,0が被るのでどちらか
                ts.add_clause(i, 0, i+1, 0)
            if overlap(li0, ri0, lj1, rj1): #0,1が被るのでどちらか
                ts.add_clause(i, 0, i+1, 1)
            if overlap(li1, ri1, lj0, rj0): #1,0が被るのでどちらか
                ts.add_clause(i, 1, i+1, 0)
            if overlap(li1, ri1, lj1, rj1): #1,1が被るのでどちらか
                ts.add_clause(i, 1, i+1, 1)
    
    if ts.satisfiable():
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()
0