結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー lilictakalilictaka
提出日時 2022-06-20 12:48:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,324 ms / 4,000 ms
コード長 4,467 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 312,664 KB
最終ジャッジ日時 2024-10-13 00:34:25
合計ジャッジ時間 40,895 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,912 KB
testcase_01 AC 40 ms
54,656 KB
testcase_02 AC 38 ms
54,784 KB
testcase_03 AC 39 ms
54,784 KB
testcase_04 AC 40 ms
54,656 KB
testcase_05 AC 40 ms
55,040 KB
testcase_06 AC 41 ms
54,912 KB
testcase_07 AC 42 ms
55,040 KB
testcase_08 AC 193 ms
80,400 KB
testcase_09 AC 222 ms
84,208 KB
testcase_10 AC 233 ms
84,820 KB
testcase_11 AC 237 ms
84,908 KB
testcase_12 AC 262 ms
83,068 KB
testcase_13 AC 971 ms
180,444 KB
testcase_14 AC 1,083 ms
187,420 KB
testcase_15 AC 1,376 ms
232,724 KB
testcase_16 AC 628 ms
203,516 KB
testcase_17 AC 1,151 ms
190,588 KB
testcase_18 AC 1,288 ms
232,484 KB
testcase_19 AC 1,548 ms
254,864 KB
testcase_20 AC 1,189 ms
223,992 KB
testcase_21 AC 1,273 ms
210,520 KB
testcase_22 AC 1,396 ms
212,120 KB
testcase_23 AC 2,001 ms
268,160 KB
testcase_24 AC 1,893 ms
267,508 KB
testcase_25 AC 1,956 ms
269,696 KB
testcase_26 AC 1,974 ms
270,300 KB
testcase_27 AC 1,922 ms
268,340 KB
testcase_28 AC 1,996 ms
269,824 KB
testcase_29 AC 2,324 ms
268,236 KB
testcase_30 AC 2,072 ms
269,136 KB
testcase_31 AC 1,947 ms
268,200 KB
testcase_32 AC 1,915 ms
265,272 KB
testcase_33 AC 41 ms
54,656 KB
testcase_34 AC 474 ms
216,888 KB
testcase_35 AC 728 ms
267,800 KB
testcase_36 AC 854 ms
282,864 KB
testcase_37 AC 42 ms
54,528 KB
testcase_38 AC 225 ms
87,948 KB
testcase_39 AC 810 ms
280,640 KB
testcase_40 AC 828 ms
312,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict,deque

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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

        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 len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
def scc_decomposition(G):
    n = len(G)
    G_rev = [[] for _ in range(n)]
    for u in range(n):
        for v in G[u]:
            G_rev[v].append(u)

    # dfs
    vs = []
    visited = [False] * n
    used = [False] * n
    for u in range(n):
        if visited[u]:
            continue
        stack = [u]
        while stack:
            v = stack.pop()
            if used[v]:
                continue
            if not visited[v]:
                visited[v] = True
            else:
                vs.append(v)
                used[v] = True
                continue
            stack.append(v)
            for c in G[v]:
                if not visited[c]:
                    stack.append(c)

    # reverse dfs
    visited = [False] * n
    component = [-1] * n
    k = 0
    for u in vs[::-1]:
        if visited[u]:
            continue
        stack = [u]
        while stack:
            v = stack.pop()
            visited[v] = True
            component[v] = k
            for c in G_rev[v]:
                if not visited[c]:
                    stack.append(c)
        k += 1

    return component
# 強連結成分分解(SCC): グラフGに対するSCCを行う
# 入力: <N>: 頂点サイズ, <G>: 順方向の有向グラフ, <RG>: 逆方向の有向グラフ
# 出力: (<ラベル数>, <各頂点のラベル番号>)
def scc(N, G, RG):
    order = []
    used = [0]*N
    group = [None]*N
    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)
    def rdfs(s, col):
        group[s] = col
        used[s] = 1
        for t in RG[s]:
            if not used[t]:
                rdfs(t, col)
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [0]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group

N,M,Q = map(int,input().split())
G0 = [[] for _ in range(N)]
for _ in range(M):
    u,v = map(lambda x:int(x)-1,input().split())
    G0[u].append(v)
    G0[v].append(u)
query = []
for _ in range(Q):
    x,y = map(lambda x:int(x)-1,input().split())
    query.append((x,y))
cmp = scc_decomposition(G0)
cmpmemo = defaultdict(list)
for i in range(N):
    cmpmemo[cmp[i]].append(i) 
G = [[] for _ in range(N)]
RG = [[] for _ in range(N)]
seen = [0] * N
for key in cmpmemo.keys():
    st = cmpmemo[key][0]
    q = deque()
    q.append((st,-1))
    while q:
        now,back = q.popleft()
        if back != -1:
            G[back].append(now)
            RG[now].append(back)
        if seen[now]:
            continue
        seen[now] += 1
        for nx in G0[now]:
            if nx == back:
                continue
            if seen[nx] <= 1:
                q.appendleft((nx,now))
cmp = scc_decomposition(G)
cnt = defaultdict(int)
for i in range(N):
    cnt[cmp[i]] += 1
uf = UnionFind(N)
G0set = [set() for _ in range(N)]
for u in range(N):
    for v in G0[u]:
        G0set[u].add(v)
        if cmp[u] == cmp[v]:
            continue
        uf.union(u,v)
for x,y in query:
    if uf.same(x,y):
        print('Yes')
    else:
        print('No')
0