結果
| 問題 |
No.416 旅行会社
|
| ユーザー |
maspy
|
| 提出日時 | 2020-03-22 12:22:59 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 960 ms / 4,000 ms |
| コード長 | 1,502 bytes |
| コンパイル時間 | 216 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 109,416 KB |
| 最終ジャッジ日時 | 2024-12-14 20:45:35 |
| 合計ジャッジ時間 | 11,269 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
class UnionFind:
def __init__(self, N):
self.root = list(range(N))
self.size = [1] * (N)
self.component = [[i] for i in range(N)]
def find_root(self, x):
root = self.root
while root[x] != x:
root[x] = root[root[x]]
x = root[x]
return x
def merge(self, x, y):
x = self.find_root(x)
y = self.find_root(y)
if x == y:
return False
sx, sy = self.size[x], self.size[y]
if sx < sy:
self.root[x] = y
self.size[y] += sx
self.component[y] += self.component[x]
else:
self.root[y] = x
self.size[x] += sy
self.component[x] += self.component[y]
return True
N, M, Q = map(int, readline().split())
m = map(int, read().split())
ABCD = tuple(zip(m, m))
AB = ABCD[:M]
CD = ABCD[M:]
init_edge = set(AB) - set(CD)
uf = UnionFind(N + 1)
find = uf.find_root
answer = [0] * (N + 1)
def merge(t, u, v):
u = find(u)
v = find(v)
r = find(1)
if u == v:
return
if r == v:
u, v = v, u
if r == u:
for i in uf.component[v]:
answer[i] = t
uf.merge(u, v)
for a, b in init_edge:
merge(-1, a, b)
for i, (a, b) in enumerate(CD[::-1]):
merge(Q - i, a, b)
print('\n'.join(map(str, answer[2:])))
maspy