結果

問題 No.517 壊れたアクセサリー
ユーザー rlangevinrlangevin
提出日時 2023-03-29 12:48:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 2,153 bytes
コンパイル時間 330 ms
コンパイル使用メモリ 81,764 KB
実行使用メモリ 53,560 KB
最終ジャッジ日時 2023-10-21 04:54:42
合計ジャッジ時間 2,124 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,560 KB
testcase_01 AC 41 ms
53,560 KB
testcase_02 AC 43 ms
53,560 KB
testcase_03 AC 42 ms
53,560 KB
testcase_04 AC 42 ms
53,560 KB
testcase_05 AC 45 ms
53,560 KB
testcase_06 AC 45 ms
53,560 KB
testcase_07 AC 42 ms
53,560 KB
testcase_08 AC 42 ms
53,560 KB
testcase_09 AC 41 ms
53,560 KB
testcase_10 AC 42 ms
53,560 KB
testcase_11 AC 42 ms
53,560 KB
testcase_12 AC 41 ms
53,560 KB
testcase_13 AC 41 ms
53,560 KB
testcase_14 AC 41 ms
53,560 KB
testcase_15 AC 42 ms
53,560 KB
testcase_16 AC 42 ms
53,560 KB
testcase_17 AC 43 ms
53,560 KB
testcase_18 AC 43 ms
53,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]

from heapq import *
def topsort(G):
    Q = []
    count = dict((u, 0) for u in range(len(G)))
 
    for u in range(len(G)):
        for v in G[u]:
            count[v] += 1
 
    temp = []
    for u in range(len(G)):
        if count[u] == 0:
            heappush(Q, u)
 
    anslst = []
    while Q:
        u = heappop(Q)
        anslst.append(u)
        temp = []
        for v in G[u]:
            count[v] -= 1
            if count[v] == 0:
                heappush(Q, v)
 
 
    anslst.reverse()
    return anslst

G = [[] for i in range(26)]
U = UnionFind(26)
N = int(input())
S = set()
for i in range(N):
    A = list(input())
    for a in A:
        S.add(a)
    for j in range(len(A) - 1):
        u, v = A[j:j+2]
        u, v = ord(u) - ord("A"), ord(v) - ord("A")
        G[u].append(v)
        U.union(u, v)

N = int(input())
for i in range(N):
    A = list(input())
    for a in A:
        S.add(a)
    for j in range(len(A) - 1):
        u, v = A[j:j+2]
        u, v = ord(u) - ord("A"), ord(v) - ord("A")
        if not G[u]:
            G[u].append(v)
        U.union(u, v)

L = topsort(G)
ans = []
for a in L:
    s = chr(a + ord("A"))
    if s in S:
        ans.append(s)
ans.reverse()
v = 0
for i in range(26):
    v = max(v, U.get_size(i))
if len(S) == len(ans) == v:
    print(*ans, sep="")
else:
    print(-1)
0