結果

問題 No.1914 Directed by Two Sequences
ユーザー chineristACchineristAC
提出日時 2022-02-06 23:11:19
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,600 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 114,224 KB
最終ジャッジ日時 2024-05-09 12:30:39
合計ジャッジ時間 14,739 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,120 KB
testcase_01 AC 41 ms
52,480 KB
testcase_02 AC 235 ms
95,620 KB
testcase_03 AC 240 ms
97,452 KB
testcase_04 AC 236 ms
97,192 KB
testcase_05 AC 239 ms
96,684 KB
testcase_06 AC 237 ms
96,292 KB
testcase_07 AC 213 ms
91,396 KB
testcase_08 AC 242 ms
94,424 KB
testcase_09 AC 239 ms
94,020 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 271 ms
107,148 KB
testcase_34 RE -
testcase_35 AC 503 ms
114,224 KB
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve(N,A,B,M,E):
    A = [a-1 for a in A]
    B = [b-1 for b in B]
    res = set()

    edge = [set() for v in range(N)]
    for u,v in E:
        edge[u-1].add(v-1)
        edge[v-1].add(u-1)
    
    group = [-1 for v in range(N)]
    for v in range(N):
        mex = [False for i in range(len(edge[v])+1)]
        for nv in edge[v]:
            if nv < v and group[nv] < len(mex):
                mex[group[nv]] = True
        for i in range(len(mex)):
            if not mex[i]:
                group[v] = i
                break

    n = max(group) + 1
    clique = [[] for g in range(n)]
    for v in range(N):
        clique[group[v]].append(v)

    def direct(i,j):
        if i < j:
            return A[i] < B[j]
        else:
            return B[i] < A[j]

    def hamilton_path(V):
        n = len(V)
        if n <= 1:
            return V

        A = hamilton_path(V[:n//2])
        B = hamilton_path(V[n//2:])
        res = []
        bi = 0
        for ai in range(len(A)):
            while bi<len(B) and direct(B[bi],A[ai]):
                res.append(B[bi])
                bi += 1
            res.append(A[ai])
        res += B[bi:]
        return res

    idx = [-1 for v in range(N)]
    for g in range(n):
        clique[g] = hamilton_path(clique[g])
        for i in range(len(clique[g])):
            idx[clique[g][i]] = i
        for u,v in zip(clique[g],clique[g][1:]):
            res.add((u,v))
    
    memo_to = [N for v in range(N)]
    memo_from = [-1 for v in range(N)]
    ci = [i for i in range(n)]
    ci.sort(lambda g:len(clique[g]))
    Vs = []

    for i in range(n):
        target = ci[i]
        Vs += clique[target]
        
        for v in Vs:
            for nv in clique[target]:
                if v==nv:
                    continue
                if direct(v,nv) and nv not in edge[v]:
                    memo_to[v] = min(memo_to[v],idx[nv])
                if direct(nv,v) and v not in edge[nv]:
                    memo_from[v] = max(memo_from[v],idx[nv])
            if memo_to[v]!=N:
                nv = clique[target][memo_to[v]]
                res.add((v,nv))      
                memo_to[v] = N
            if memo_from[v]!=-1:
                pv = clique[target][memo_from[v]]
                res.add((pv,v))
                memo_from[v] = -1
    
    print(len(res))
    for u,v in res:
        print(u+1,v+1)

import sys

input = lambda :sys.stdin.buffer.readline()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

N,M = mi()
assert  N<= 5000
A = li()
B = li()
E = [tuple(mi()) for i in range(M)]
solve(N,A,B,M,E)
0