結果
問題 | No.274 The Wall |
ユーザー |
|
提出日時 | 2024-06-08 01:11:13 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 211 ms / 2,000 ms |
コード長 | 2,078 bytes |
コンパイル時間 | 450 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 79,232 KB |
最終ジャッジ日時 | 2024-12-26 14:19:07 |
合計ジャッジ時間 | 4,995 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 22 |
ソースコード
#https://tjkendev.github.io/procon-library/python/graph/2-sat.htmlfrom collections import dequedef scc(N, G, RG):order = []used = [0]*Ndef dfs(s):used[s] = 1for t in G[s]:if not used[t]:dfs(t)order.append(s)for i in range(N):if not used[i]:dfs(i)group = [-1]*Nlabel = 0order.reverse()for s in order:if group[s] != -1:continueque = deque([s])group[s] = labelwhile que:v = que.popleft()for w in RG[v]:if group[w] != -1:continueque.append(w)group[w] = labellabel += 1return group # topological orderingN,M = map(int, input().split())G = [[] for i in range(2*M)]RG = [[] for i in range(2*M)]# add (a ∨ b)# a = x_i if neg_i = 0# a = ~x_i if neg_i = 1def add_edge(i, neg_i, j, neg_j):##print(i,neg_i,j,neg_j)if neg_i:i0 = i+M; i1 = ielse:i0 = i; i1 = i+Mif neg_j:j0 = j+M; j1 = jelse:j0 = j; j1 = j+M# add (~a ⇒ b)G[i1].append(j0); RG[j0].append(i1)# add (~b ⇒ a)G[j1].append(i0); RG[i0].append(j1)# check if the formula is satisfiabledef check(group):for i in range(M):if group[i] == group[i+M]:return Falsereturn Truel = [tuple(map(int, input().split())) for i in range(N)]for i in range(N):L1,R1 = l[i]r_R1,r_L1 = M-L1-1,M-R1-1for j in range(i+1,N):L2,R2 = l[j]flag1,flag2 = False,Falser_R2,r_L2 = M-L2-1,M-R2-1if (L1 <= R2 and L2 <= R1):flag1 = Trueadd_edge(i, 0, j, 0)add_edge(i, 1, j, 1)if (L1 <= r_R2 and r_L2 <= R1):flag2 = Trueadd_edge(i, 0, j, 1)add_edge(i, 1, j, 0)if flag1 and flag2:print("NO")exit()group = scc(2*M, G, RG)if check(group):print("YES")else:print("NO")