結果

問題 No.1382 Travel in Mitaru city
ユーザー 👑 rin204rin204
提出日時 2022-04-16 16:15:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 512 ms / 2,000 ms
コード長 1,974 bytes
コンパイル時間 514 ms
コンパイル使用メモリ 86,956 KB
実行使用メモリ 113,356 KB
最終ジャッジ日時 2023-08-26 19:05:55
合計ジャッジ時間 23,104 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,196 KB
testcase_01 AC 64 ms
71,428 KB
testcase_02 AC 65 ms
71,172 KB
testcase_03 AC 65 ms
71,140 KB
testcase_04 AC 65 ms
71,024 KB
testcase_05 AC 62 ms
71,324 KB
testcase_06 AC 203 ms
82,668 KB
testcase_07 AC 197 ms
81,856 KB
testcase_08 AC 190 ms
81,620 KB
testcase_09 AC 204 ms
82,788 KB
testcase_10 AC 190 ms
82,772 KB
testcase_11 AC 358 ms
99,352 KB
testcase_12 AC 387 ms
99,724 KB
testcase_13 AC 381 ms
98,496 KB
testcase_14 AC 417 ms
99,388 KB
testcase_15 AC 403 ms
99,564 KB
testcase_16 AC 475 ms
112,416 KB
testcase_17 AC 485 ms
112,680 KB
testcase_18 AC 512 ms
112,424 KB
testcase_19 AC 492 ms
112,400 KB
testcase_20 AC 477 ms
112,684 KB
testcase_21 AC 467 ms
112,368 KB
testcase_22 AC 470 ms
112,488 KB
testcase_23 AC 470 ms
112,412 KB
testcase_24 AC 446 ms
112,352 KB
testcase_25 AC 481 ms
112,408 KB
testcase_26 AC 486 ms
94,908 KB
testcase_27 AC 441 ms
94,584 KB
testcase_28 AC 394 ms
94,944 KB
testcase_29 AC 464 ms
94,940 KB
testcase_30 AC 371 ms
94,728 KB
testcase_31 AC 368 ms
91,772 KB
testcase_32 AC 358 ms
94,988 KB
testcase_33 AC 319 ms
94,376 KB
testcase_34 AC 311 ms
87,892 KB
testcase_35 AC 263 ms
86,380 KB
testcase_36 AC 164 ms
92,412 KB
testcase_37 AC 112 ms
78,948 KB
testcase_38 AC 168 ms
94,236 KB
testcase_39 AC 120 ms
82,428 KB
testcase_40 AC 150 ms
88,800 KB
testcase_41 AC 162 ms
90,764 KB
testcase_42 AC 178 ms
94,516 KB
testcase_43 AC 171 ms
91,976 KB
testcase_44 AC 125 ms
83,816 KB
testcase_45 AC 159 ms
90,436 KB
testcase_46 AC 135 ms
82,048 KB
testcase_47 AC 187 ms
92,416 KB
testcase_48 AC 189 ms
87,412 KB
testcase_49 AC 208 ms
94,356 KB
testcase_50 AC 164 ms
84,440 KB
testcase_51 AC 399 ms
108,664 KB
testcase_52 AC 446 ms
113,356 KB
testcase_53 AC 232 ms
86,636 KB
testcase_54 AC 325 ms
95,296 KB
testcase_55 AC 459 ms
112,264 KB
testcase_56 AC 275 ms
90,116 KB
testcase_57 AC 369 ms
102,132 KB
testcase_58 AC 223 ms
82,724 KB
testcase_59 AC 267 ms
89,832 KB
testcase_60 AC 426 ms
112,156 KB
testcase_61 AC 99 ms
77,796 KB
testcase_62 AC 65 ms
71,244 KB
testcase_63 AC 125 ms
78,128 KB
testcase_64 AC 106 ms
78,136 KB
testcase_65 AC 79 ms
75,372 KB
testcase_66 AC 97 ms
77,476 KB
testcase_67 AC 106 ms
78,196 KB
testcase_68 AC 101 ms
77,532 KB
testcase_69 AC 102 ms
78,256 KB
testcase_70 AC 112 ms
78,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n, m, s, t = map(int, input().split())
P = list(map(int, input().split()))
s -= 1
t -= 1
edges = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if P[a] > P[b]:
        a, b = b, a
    edges[a].append(b)
    
dic = {}
for i, p in enumerate(P):
    if p not in dic:
        dic[p] = []
    dic[p].append(i)
    
ans = 0
UF = UnionFind(n)
for p in sorted(dic.keys(), reverse = True):
    flg = False
    tmp = []
    for u in dic[p]:
        for v in edges[u]:
            if P[v] != P[u] and UF.same(v, s):
                flg = True

    if flg:
        ans += 1
    for u in dic[p]:
        for v in edges[u]:
            UF.union(u, v)
    
print(ans)



0