結果
| 問題 | No.1983 [Cherry 4th Tune C] 南の島のマーメイド |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-06-21 18:55:26 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,203 ms / 4,000 ms |
| コード長 | 2,202 bytes |
| 記録 | |
| コンパイル時間 | 205 ms |
| コンパイル使用メモリ | 82,380 KB |
| 実行使用メモリ | 139,316 KB |
| 最終ジャッジ日時 | 2024-10-14 13:20:35 |
| 合計ジャッジ時間 | 31,353 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 41 |
ソースコード
class Unionfind:
def __init__(self,n):
self.uf = [-1]*n
def find(self,x):
if self.uf[x] < 0:
return x
else:
self.uf[x] = self.find(self.uf[x])
return self.uf[x]
def same(self,x,y):
return self.find(x) == self.find(y)
def union(self,x,y):
x = self.find(x)
y = self.find(y)
if x == y:
return False
if self.uf[x] > self.uf[y]:
x,y = y,x
self.uf[x] += self.uf[y]
self.uf[y] = x
return True
def size(self,x):
x = self.find(x)
return -self.uf[x]
def Lowlink(graph):
n = len(graph)
order = [-1]*n
low = [n]*n
par = [-1]*n
vis = 0
for root in range(n):
if order[root] != -1:
continue
q = [root]
while q:
now = q.pop()
if now < 0:
now = ~now
if now == root:
continue
p = par[now]
low[p] = min(low[p],low[now])
continue
if order[now] >= 0:
continue
order[now] = vis
low[now] = vis
vis += 1
q.append(~now)
check = 0
for nex in graph[now]:
if par[now] == nex and check == 0:
check += 1
continue
elif order[nex] >= 0:
if low[now] < 0 or order[nex] < low[now]:
low[now] = order[nex]
else:
par[nex] = now
q.append(nex)
briges = []
for i in range(n):
if par[i] < 0:
continue
if low[i] < 0 or low[i] > order[par[i]]:
briges.append((i,par[i]))
return briges
n,m,q = map(int,input().split())
e = [[] for i in range(n)]
for _ in range(m):
u,v = [int(x)-1 for x in input().split()]
e[u].append(v)
e[v].append(u)
briges = Lowlink(e)
uf = Unionfind(n)
for u,v in briges:
uf.union(u,v)
for _ in range(q):
x,y = [int(i)-1 for i in input().split()]
print("Yes" if uf.same(x,y) else "No")