結果
問題 | No.274 The Wall |
ユーザー |
![]() |
提出日時 | 2024-02-04 13:51:01 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 195 ms / 2,000 ms |
コード長 | 3,430 bytes |
コンパイル時間 | 328 ms |
コンパイル使用メモリ | 82,068 KB |
実行使用メモリ | 79,212 KB |
最終ジャッジ日時 | 2024-09-28 11:15:56 |
合計ジャッジ時間 | 3,307 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 22 |
ソースコード
import syssys.setrecursionlimit(10**7)class DirectedGraph():def __init__(self, N):self.N = Nself.G = [[] for i in range(N)]self.rG = [[] for i in range(N)]self.order = []self.group = [-1] * Nself.label = 0def add_edge(self, u, v):self.G[u].append(v)self.rG[v].append(u)def dfs(self, s, used):used[s] = 1for u in self.G[s]:if used[u]:continueself.dfs(u, used)self.order.append(s)def rdfs(self, s, num, used):self.group[s] = numused[s] = 1for u in self.rG[s]:if used[u]:continueself.rdfs(u, num, used)def scc(self):used = [0] * self.Nfor i in range(self.N):if used[i]:continueself.dfs(i, used)used = [0] * self.Nfor s in reversed(self.order):if used[s]:continueself.rdfs(s, self.label, used)self.label += 1return self.label, self.groupdef construct(self):nG = [set() for _ in range(self.label)]mem = [[] for i in range(self.label)]for s in range(self.N):now = self.group[s]for u in self.G[s]:if now == self.group[u]:continuenG[now].add(self.group[u])mem[now].append(s)return nG, memclass TwoSAT():def __init__(self, N):self.G = DirectedGraph(2 * N)def add(self, x1, x2, f1, f2):if f1 == True and f2 == True:# ¬x1∪¬x2# (x1⇒¬x2)∩(x2⇒¬x1)self.G.add_edge(x1, x2 + N)self.G.add_edge(x2, x1 + N)if f1 == True and f2 == False:# ¬x1∪x2# (x1⇒x2)∩(¬x2⇒¬x1)self.G.add_edge(x1, x2)self.G.add_edge(x2 + N, x1 + N)if f1 == False and f2 == True:# x1∪¬x2# (¬x1⇒¬x2)∩(x2⇒x1)self.G.add_edge(x1 + N, x2 + N)self.G.add_edge(x2, x1)if f1 == False and f2 == False:# x1∪x2# (¬x1⇒x2)∩(¬x2⇒x1)self.G.add_edge(x1 + N, x2)self.G.add_edge(x2 + N, x1)def check(self):_, group = self.G.scc()for i in range(N):if group[i] == group[i + N]:return Falsereturn TrueN, M = map(int, input().split())L, R = [0] * N, [0] * Ncnt = 0for i in range(N):L[i], R[i] = map(int, input().split())cnt += R[i] - L[i] + 1if cnt > M:print("NO")exit()def check(L1, R1, L2, R2):if R1 < L2 or R2 < L1:return Truereturn FalseTS = TwoSAT(N)for i in range(N):for j in range(i + 1, N):if not check(L[i], R[i], L[j], R[j]):TS.add(i, j, True, True)if not check(L[i], R[i], M - 1 - R[j], M - 1 - L[j]):TS.add(i, j, True, False)if not check(M - 1 - R[i], M - 1 - L[i], L[j], R[j]):TS.add(i, j, False, True)if not check(M - 1 - R[i], M - 1 - L[i], M - 1 - R[j], M - 1 - L[j]):TS.add(i, j, False, False)print("YES") if TS.check() else print("NO")