結果

問題 No.1914 Directed by Two Sequences
ユーザー chineristACchineristAC
提出日時 2021-10-31 18:48:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,204 bytes
コンパイル時間 775 ms
コンパイル使用メモリ 82,212 KB
実行使用メモリ 219,816 KB
最終ジャッジ日時 2024-05-09 12:18:02
合計ジャッジ時間 43,699 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 39 ms
54,760 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self, init_val, segfunc, ide_ele):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        self.size = n
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        k += self.num
        self.tree[k] = x
        while k > 1:
            k >>= 1
            self.tree[k] = self.segfunc(self.tree[2*k], self.tree[2*k+1])

    def query(self, l, r):
        if r==self.size:
            r = self.num

        res = self.ide_ele

        l += self.num
        r += self.num
        right = []
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                right.append(self.tree[r-1])
            l >>= 1
            r >>= 1

        for e in right[::-1]:
            res = self.segfunc(res,e)
        return res

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

    edge = [[] for v in range(N)]
    for u,v in E:
        edge[u-1].append(v-1)
        edge[v-1].append(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))
    
    if 5*10**4 <= N:
        import random
        E = set(E)
        while len(res) < 7 * 10**5:
            u = random.randint(1,N-1)
            v = random.randint(u+1,N)
            if (u,v) in E:
                continue
            if direct(u-1,v-1):
                res.add((u-1,v-1))
            else:
                res.add((v-1,u-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()
A = li()
B = li()
E = [tuple(mi()) for i in range(M)]
solve(N,A,B,M,E)
0