結果
| 問題 |
No.1382 Travel in Mitaru city
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-02-05 20:11:21 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 673 ms / 2,000 ms |
| コード長 | 1,727 bytes |
| コンパイル時間 | 152 ms |
| コンパイル使用メモリ | 82,264 KB |
| 実行使用メモリ | 110,148 KB |
| 最終ジャッジ日時 | 2024-07-04 03:38:52 |
| 合計ジャッジ時間 | 25,304 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 68 |
ソースコード
class UnionFind:
def __init__(self, n):
self.parent = [-1] * n
self.rank = [1] * n
self.count = [1] * n
def get_root(self, i):
while self.parent[i] != -1:
i = self.parent[i]
return i
def is_same_tree(self, i, j):
return self.get_root(i) == self.get_root(j)
def merge(self, i, j):
i = self.get_root(i)
j = self.get_root(j)
if i == j:
return
if self.rank[i] > self.rank[j]:
self.parent[j] = i
self.rank[i] = max(self.rank[i], self.rank[j] + 1)
self.count[i] += self.count[j]
else:
self.parent[i] = j
self.rank[j] = max(self.rank[j], self.rank[i] + 1)
self.count[j] += self.count[i]
n, m, s, t = map(int, input().split())
s -= 1
t -= 1
p = [int(i) for i in input().split()]
pd = {}
for ind, i in enumerate(p):
if i not in pd:
pd[i] = []
pd[i].append(ind)
e = [[] for _ in range(n)]
for _ in range(m):
a, b = [int(i) - 1 for i in input().split()]
e[a].append(b)
e[b].append(a)
uf = UnionFind(n)
dp = [-float("inf")] * n
used = [False] * n
for p_key in sorted(pd.keys(), reverse=True):
a = []
for i in pd[p_key]:
if i == s:
a.append(0)
else:
aa = -float("inf")
for j in e[i]:
if used[j]:
aa = max(aa, dp[uf.get_root(j)])
a.append(aa+1)
for i in pd[p_key]:
used[i] = True
for j in e[i]:
if used[j]:
uf.merge(i, j)
for i, aa in zip(pd[p_key], a):
r = uf.get_root(i)
dp[r] = max(dp[r], aa)
print(dp[uf.get_root(t)])