結果

問題 No.2319 Friends+
ユーザー prin_kemkemprin_kemkem
提出日時 2023-05-27 20:46:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,918 bytes
コンパイル時間 1,158 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 93,276 KB
最終ジャッジ日時 2023-08-27 00:07:10
合計ジャッジ時間 23,793 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 167 ms
81,312 KB
testcase_01 AC 166 ms
80,976 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 396 ms
91,528 KB
testcase_05 WA -
testcase_06 AC 396 ms
91,324 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 173 ms
81,064 KB
testcase_18 AC 340 ms
85,888 KB
testcase_19 AC 374 ms
92,260 KB
testcase_20 AC 425 ms
86,740 KB
testcase_21 AC 415 ms
86,632 KB
testcase_22 AC 412 ms
86,904 KB
testcase_23 AC 386 ms
89,276 KB
testcase_24 AC 405 ms
87,656 KB
testcase_25 AC 397 ms
87,468 KB
testcase_26 AC 408 ms
86,768 KB
testcase_27 AC 410 ms
86,544 KB
testcase_28 AC 414 ms
86,748 KB
testcase_29 AC 426 ms
87,100 KB
testcase_30 AC 422 ms
86,160 KB
testcase_31 AC 414 ms
86,660 KB
testcase_32 AC 425 ms
86,364 KB
testcase_33 AC 428 ms
87,744 KB
testcase_34 AC 426 ms
86,484 KB
testcase_35 AC 432 ms
86,744 KB
testcase_36 AC 375 ms
92,180 KB
testcase_37 AC 354 ms
90,224 KB
testcase_38 AC 435 ms
92,232 KB
testcase_39 AC 428 ms
92,368 KB
testcase_40 AC 428 ms
91,588 KB
testcase_41 AC 422 ms
92,036 KB
testcase_42 AC 435 ms
91,264 KB
testcase_43 AC 421 ms
92,076 KB
testcase_44 AC 418 ms
91,100 KB
testcase_45 AC 407 ms
91,336 KB
testcase_46 AC 344 ms
90,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque, Counter
import copy
from itertools import combinations, permutations, product, accumulate, groupby
from heapq import heapify, heappop, heappush
import math
import bisect
from pprint import pprint
from random import randint
import sys
# sys.setrecursionlimit(700000)
input = lambda: sys.stdin.readline().rstrip('\n')
inf = float('inf')
mod1 = 10**9+7
mod2 = 998244353
def ceil_div(x, y): return -(-x//y)

#################################################

class UnionFind:
    #コンストラクタ
    def __init__(self, n):
        self.n = n
        self.parents = [-1]*n

    #点xの根を調べる+親が根になるよう移動
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    #点x,yの属する集合同士を連結(要素数が少ない方を多い方に連結)
    #辺を追加したらTrue, しなければFalseを返す
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        return True

    #点xが属する集合の要素数を取得
    def size(self, x):
        return -self.parents[self.find(x)]

    #点x,yが同じ集合に属しているか判定
    def same(self, x, y):
        return self.find(x) == self.find(y)

    #点xの属する集合の全要素を取得
    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 get_groups(self):
        groups = defaultdict(list)
        for x in range(self.n):
            groups[self.find(x)].append(x)
        return groups

    #print(インスタンス)で、全集合の「根と全要素」を出力
    def __str__(self):
        return '\n'.join('{}:{}'.format(r, self.menbers(r)) for r in self.roots())

N, M = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
members = [defaultdict(int) for _ in range(N)]
uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    uf.union(a, b)
for i, p in enumerate(P):
    members[p][uf.find(i)] += 1
Q = int(input())
for _ in range(Q):
    x, y = map(int, input().split())
    x -= 1; y -= 1
    if P[x] != P[y] and members[P[y]][uf.find(x)] > 0:
        print("Yes")
        members[P[x]][uf.find(x)] -= 1
        members[P[y]][uf.find(x)] += 1
        P[x] = P[y]
    else:
        print("No")
0