結果

問題 No.2346 Replace!!
ユーザー navel_tosnavel_tos
提出日時 2023-06-09 23:56:38
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 6,436 bytes
コンパイル時間 869 ms
コンパイル使用メモリ 87,180 KB
実行使用メモリ 83,420 KB
最終ジャッジ日時 2023-08-30 15:02:54
合計ジャッジ時間 18,042 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
73,040 KB
testcase_01 WA -
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 AC 114 ms
73,012 KB
testcase_29 AC 144 ms
79,988 KB
testcase_30 AC 138 ms
79,484 KB
testcase_31 AC 210 ms
80,916 KB
testcase_32 WA -
testcase_33 AC 208 ms
80,728 KB
testcase_34 AC 203 ms
80,828 KB
testcase_35 WA -
testcase_36 AC 182 ms
80,328 KB
testcase_37 AC 183 ms
80,124 KB
testcase_38 AC 187 ms
80,064 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 219 ms
80,716 KB
testcase_44 WA -
testcase_45 AC 183 ms
81,040 KB
testcase_46 AC 177 ms
80,356 KB
testcase_47 AC 185 ms
80,580 KB
testcase_48 AC 173 ms
80,504 KB
testcase_49 AC 202 ms
80,712 KB
testcase_50 AC 191 ms
80,128 KB
testcase_51 AC 146 ms
79,584 KB
testcase_52 WA -
testcase_53 WA -
testcase_54 AC 147 ms
80,132 KB
testcase_55 AC 145 ms
79,964 KB
testcase_56 AC 147 ms
80,212 KB
testcase_57 AC 147 ms
79,896 KB
testcase_58 AC 145 ms
79,972 KB
testcase_59 AC 147 ms
80,016 KB
testcase_60 AC 143 ms
79,992 KB
testcase_61 AC 142 ms
80,504 KB
testcase_62 AC 141 ms
80,152 KB
testcase_63 AC 144 ms
80,088 KB
testcase_64 AC 146 ms
80,536 KB
testcase_65 AC 146 ms
80,132 KB
testcase_66 WA -
testcase_67 AC 147 ms
79,820 KB
testcase_68 AC 143 ms
80,528 KB
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#yukicoder392D
'''
重複がない場合、完全一致していないと答えを合わせることはできない。
そうでない場合は、なんだろう、閉路が重要そうだな。

有向辺を辿って目的の数字に行けることは必要条件。
問題となるのは閉路のループがある場合だろう。これAGCのボス問でみたぞ。

えー 現状有向辺を辿るだけなのですが 真か?
違うか 有向辺を辿りつつも、辿った有向辺は削除しないといけないわけで
となると閉路の部分をどうするかが鍵になりそうだ

閉路とは?書いてある初期数が循環している値たちのことを指す
数字が一致してもしゃあないので 結局は閉路どうする?になる

閉路内から別の頂点に移動することはできるか?
むりですね だって閉路だから

SCCして各頂点を辿ることで目的地に到達できるか?
閉路内の頂点は、目的地の重複があれば常に大丈夫・・・なのかな

とりあえずSCCしてみるか(思考停止)

提出。TLEは絶対しなさそうだった。
'''
#SCC Reference: https://atcoder.jp/contests/typical90/submissions/25180888
class scc_graph:
    def __init__(self, N):
        self.N = N
        self.edges = []
 
    def csr(self):
        self.start = [0]*(self.N+1)
        self.elist = [0]*len(self.edges)
        for e in self.edges:
            self.start[e[0]+1] += 1
        for i in range(1, self.N+1):
            self.start[i] += self.start[i-1]
        counter = self.start[:]
        for e in self.edges:
            self.elist[counter[e[0]]] = e[1]
            counter[e[0]] += 1
 
    def add_edge(self, v, w):
        self.edges.append((v, w))
 
    def scc_ids(self):
        self.csr()
        N = self.N
        now_ord = group_num = 0
        visited = []
        low = [0]*N
        order = [-1]*N
        ids = [0]*N
        parent = [-1]*N
        stack = []
        for i in range(N):
            if order[i] == -1:
                stack.append(i)
                stack.append(i)
                while stack:
                    v = stack.pop()
                    if order[v] == -1:
                        low[v] = order[v] = now_ord
                        now_ord += 1
                        visited.append(v)
                        for i in range(self.start[v], self.start[v+1]):
                            to = self.elist[i]
                            if order[to] == -1:
                                stack.append(to)
                                stack.append(to)
                                parent[to] = v
                            else:
                                low[v] = min(low[v], order[to])
                    else:
                        if low[v] == order[v]:
                            while True:
                                u = visited.pop()
                                order[u] = N
                                ids[u] = group_num
                                if u == v:
                                    break
                            group_num += 1
                        if parent[v] != -1:
                            low[parent[v]] = min(low[parent[v]], low[v])
        for i, x in enumerate(ids):
            ids[i] = group_num-1-x
 
        return group_num, ids
 
    def scc(self):
        group_num, ids = self.scc_ids()
        groups = [[] for _ in range(group_num)]
        for i, x in enumerate(ids):
            groups[x].append(i)
        return groups

from collections import deque as dq


for _ in range(int(input())):
    N=int(input()); A=list(map(lambda x:x-1,map(int,input().split())))
    B=list(map(lambda x:x-1,map(int,input().split())))
    SCC=scc_graph(N)
    for i in range(N): SCC.add_edge(i,A[i])
    X=SCC.scc(); visited=[0]*N; hantei=1; closed=set()
    for St in X:
        if len(St)>1:
            for i in St: closed.add(i)
    for St in X:
        if not hantei: break
        if len(St)>1:
            for i in St:
                if i!=B[i]: break
            else: continue
            P=set(St); Q=set(); cnt=0
            for now in St:
                if B[now] not in P: hantei=0; break
                Q.add(B[now]); visited[now]=1
                if now==B[now]: cnt+=1
            if len(P)==len(Q) or cnt>=2: hantei=0; break
        elif visited[St[0]]: continue
        elif len(St)==1:
            now=St[0]; visit=[now]; arrive=set()
            while 1:
                now=A[now]
                if now not in arrive: arrive.add(now); visit.append(now)
                else: break
            for i in visit:
                visited[i]=1
                if B[i] in arrive: arrive.discard(i)
                elif i not in closed: hantei=0; break
    print('Yes') if hantei else print('No')
                

def solve(N,A,B):
    SCC=scc_graph(N)
    for i in range(N): SCC.add_edge(i,A[i])
    X=SCC.scc(); visited=[0]*N; hantei=1; closed=set()
    for St in X:
        if len(St)>1:
            for i in St: closed.add(i)
    for St in X:
        if not hantei: break
        if len(St)>1:
            P=set(St); Q=set()
            for now in St:
                if B[now] not in P: hantei=0; break
                Q.add(B[now]); visited[now]=1
            if len(P)==len(Q): hantei=0; break
        elif visited[St[0]]: continue
        elif len(St)==1:
            now=St[0]; visit=[now]; arrive=set()
            while 1:
                now=A[now]
                if now not in arrive: arrive.add(now); visit.append(now)
                else: break
            for i in visit:
                visited[i]=1
                if B[i] in arrive: arrive.discard(i)
                elif i not in closed: hantei=0; break
    return hantei

from random import randint as ri, shuffle as rs
def Testcase(N):
    A=[ri(0,N-1) for _ in range(N)]; B=[ri(0,N-1) for _ in range(N)]; return A,B
def Naive(N,A,B):
    if A==B: return 1
    for _ in range(1000):
        X,Y=A[:],B[:]
        for _ in range(100):
            x=ri(0,N-1); X[x]=X[X[x]]
            if X==Y: return 1
    return 0
def check(N,time):
    for _ in range(time):
        A,B=Testcase(N); X=Naive(N,A,B); Y=solve(N,A,B)
        if X!=Y: print(A,B,X,Y)
def check2(N,time):
    for _ in range(time):
        _,B=Testcase(N); A=[i for i in range(N)]; rs(A)
        X=Naive(N,A,B); Y=solve(N,A,B)
        if X!=Y: print(A,B,X,Y)
0