結果

問題 No.2319 Friends+
ユーザー prin_kemkemprin_kemkem
提出日時 2023-05-27 20:17:48
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,914 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 93,312 KB
最終ジャッジ日時 2023-08-26 23:54:20
合計ジャッジ時間 26,339 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 187 ms
81,236 KB
testcase_01 AC 181 ms
81,132 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 435 ms
91,220 KB
testcase_05 WA -
testcase_06 AC 431 ms
90,924 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 191 ms
81,140 KB
testcase_18 AC 360 ms
85,760 KB
testcase_19 AC 385 ms
92,308 KB
testcase_20 AC 434 ms
86,856 KB
testcase_21 AC 435 ms
86,832 KB
testcase_22 AC 431 ms
86,980 KB
testcase_23 AC 406 ms
88,644 KB
testcase_24 AC 418 ms
87,868 KB
testcase_25 AC 416 ms
87,268 KB
testcase_26 AC 430 ms
86,908 KB
testcase_27 AC 434 ms
86,732 KB
testcase_28 AC 434 ms
86,504 KB
testcase_29 AC 442 ms
86,908 KB
testcase_30 AC 434 ms
86,436 KB
testcase_31 AC 431 ms
86,116 KB
testcase_32 AC 441 ms
86,560 KB
testcase_33 AC 472 ms
87,528 KB
testcase_34 AC 435 ms
86,408 KB
testcase_35 AC 442 ms
86,620 KB
testcase_36 AC 389 ms
92,352 KB
testcase_37 AC 366 ms
90,304 KB
testcase_38 AC 451 ms
92,364 KB
testcase_39 AC 444 ms
92,492 KB
testcase_40 AC 446 ms
91,620 KB
testcase_41 AC 432 ms
91,852 KB
testcase_42 AC 440 ms
91,344 KB
testcase_43 AC 449 ms
91,596 KB
testcase_44 AC 515 ms
90,556 KB
testcase_45 AC 429 ms
91,272 KB
testcase_46 AC 360 ms
90,600 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)]:
        print("Yes")
        members[P[x]][uf.find(x)] -= 1
        members[P[y]][uf.find(x)] += 1
        P[x] = P[y]
    else:
        print("No")
0