結果

問題 No.274 The Wall
ユーザー H20
提出日時 2022-03-07 12:45:46
言語 PyPy3
(7.3.15)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,590 bytes
コンパイル時間 379 ms
コンパイル使用メモリ 82,420 KB
実行使用メモリ 856,228 KB
最終ジャッジ日時 2024-07-22 06:40:37
合計ジャッジ時間 4,312 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other AC * 3 MLE * 1 -- * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

def two_sat(n,clause):
    answer=[0]*n
    edges=[]
    N=2*n
    for s in clause:
        i,f,j,g=s
        edges.append((2*i+(0 if f else 1),2*j+(1 if g else 0)))
        edges.append((2*j+(0 if g else 1),2*i+(1 if f else 0)))
    M=len(edges)
    start=[0]*(N+1)
    elist=[0]*M
    for e in edges:
        start[e[0]+1]+=1
    for i in range(1,N+1):
        start[i]+=start[i-1]
    counter=start[:]
    for e in edges:
        elist[counter[e[0]]]=e[1]
        counter[e[0]]+=1
    visited=[]
    low=[0]*N
    Ord=[-1]*N
    ids=[0]*N
    NG=[0,0]
    def dfs(v):
        stack=[(v,-1,0),(v,-1,1)]
        while stack:
            v,bef,t=stack.pop()
            if t:
                if bef!=-1 and Ord[v]!=-1:
                    low[bef]=min(low[bef],Ord[v])
                    stack.pop()
                    continue
                low[v]=NG[0]
                Ord[v]=NG[0]
                NG[0]+=1
                visited.append(v)
                for i in range(start[v],start[v+1]):
                    to=elist[i]
                    if Ord[to]==-1:
                        stack.append((to,v,0))
                        stack.append((to,v,1))
                    else:
                        low[v]=min(low[v],Ord[to])
            else:
                if low[v]==Ord[v]:
                    while(True):
                        u=visited.pop()
                        Ord[u]=N
                        ids[u]=NG[1]
                        if u==v:
                            break
                    NG[1]+=1
                low[bef]=min(low[bef],low[v])
    for i in range(N):
        if Ord[i]==-1:
            dfs(i)
    for i in range(N):
        ids[i]=NG[1]-1-ids[i]
    for i in range(n):
        if ids[2*i]==ids[2*i+1]:
            return None
        answer[i]=(ids[2*i]<ids[2*i+1])
    return answer

N,M = map(int,input().split())
LR = [list(map(int,input().split())) for _ in range(N)]
clause=[]
for i in range(N):
    for j in range(i+1,N):
        IFl = LR[i][0]
        IFr = LR[i][1]
        JFl = LR[j][0]
        JFr = LR[j][1]
        IBl = M-LR[i][1]-1
        IBr = M-LR[i][0]-1
        JBl = M-LR[j][1]-1
        JBr = M-LR[j][0]-1

        if (IFl <= JFr and JFl <= IFr):
            clause.append((i,True,j,True))
        if (IFl <= JBr and JBl <= IFr):
            clause.append((i,True,j,False))
        if (IBl <= JFr and JFl <= IBr):
            clause.append((i,False,j,True))
        if (IBl <= JBr and JBl <= IBr):
            clause.append((i,False,j,False))

ans=two_sat(N,clause)
if ans==None:
    print('NO')
else:
    print('YES')
0