結果

問題 No.2319 Friends+
ユーザー ShirotsumeShirotsume
提出日時 2023-05-26 22:44:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,706 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 87,236 KB
実行使用メモリ 88,168 KB
最終ジャッジ日時 2023-08-26 12:55:53
合計ジャッジ時間 17,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,472 KB
testcase_01 AC 80 ms
71,732 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 270 ms
86,692 KB
testcase_05 WA -
testcase_06 AC 267 ms
86,880 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 91 ms
72,424 KB
testcase_13 AC 94 ms
72,196 KB
testcase_14 AC 91 ms
72,344 KB
testcase_15 AC 89 ms
72,484 KB
testcase_16 AC 94 ms
72,192 KB
testcase_17 AC 84 ms
71,980 KB
testcase_18 AC 238 ms
82,096 KB
testcase_19 AC 267 ms
88,040 KB
testcase_20 AC 347 ms
82,208 KB
testcase_21 AC 318 ms
81,800 KB
testcase_22 AC 324 ms
82,108 KB
testcase_23 AC 289 ms
84,840 KB
testcase_24 AC 301 ms
83,768 KB
testcase_25 AC 308 ms
82,464 KB
testcase_26 AC 311 ms
82,028 KB
testcase_27 AC 302 ms
81,844 KB
testcase_28 AC 312 ms
81,928 KB
testcase_29 AC 307 ms
81,836 KB
testcase_30 AC 307 ms
81,764 KB
testcase_31 AC 324 ms
81,672 KB
testcase_32 AC 335 ms
83,048 KB
testcase_33 AC 321 ms
83,252 KB
testcase_34 AC 315 ms
82,544 KB
testcase_35 AC 319 ms
83,136 KB
testcase_36 AC 252 ms
88,168 KB
testcase_37 AC 244 ms
86,308 KB
testcase_38 AC 325 ms
87,488 KB
testcase_39 AC 316 ms
87,332 KB
testcase_40 AC 320 ms
87,104 KB
testcase_41 AC 327 ms
87,108 KB
testcase_42 AC 332 ms
87,452 KB
testcase_43 AC 324 ms
86,880 KB
testcase_44 AC 322 ms
86,908 KB
testcase_45 AC 312 ms
86,920 KB
testcase_46 AC 237 ms
86,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque, Counter
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353

class dsu():
    n=1
    parent_or_size=[-1 for i in range(n)]
    def __init__(self,N):
        self.n=N
        self.num = N
        self.parent_or_size=[-1 for i in range(N)]
    def merge(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        x=self.leader(a)
        y=self.leader(b)
        if x==y:
            return x
        self.num -= 1
        if (-self.parent_or_size[x]<-self.parent_or_size[y]):
            x,y=y,x
        self.parent_or_size[x]+=self.parent_or_size[y]
        self.parent_or_size[y]=x
        return x
    def same(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        return self.leader(a)==self.leader(b)
    def leader(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        if (self.parent_or_size[a]<0):
            return a
        self.parent_or_size[a]=self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]
    def size(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        return -self.parent_or_size[self.leader(a)]
    def groups(self):
        leader_buf=[0 for i in range(self.n)]
        group_size=[0 for i in range(self.n)]
        for i in range(self.n):
            leader_buf[i]=self.leader(i)
            group_size[leader_buf[i]]+=1
        result=[[] for i in range(self.n)]
        for i in range(self.n):
            result[leader_buf[i]].append(i)
        result2=[]
        for i in range(self.n):
            if len(result[i])>0:
                result2.append(result[i])
        return result2
    def count(self):
        return self.num

n, m = mi()
if m > 50000:
    nowworld = [v - 1 for v in li()] #n人の今いるワールド

    worldleaderlist = [Counter() for _ in range(n)]
    U = dsu(n)
    for _ in range(m):
        u, v = mi()
        u -= 1; v -= 1
        U.merge(u, v)

    for i in range(n):
        worldleaderlist[nowworld[i]][U.leader(i)] += 1

    for _ in range(ii()):
        x, y = mi()
        x -= 1; y -= 1
        if nowworld[x] != nowworld[y] and worldleaderlist[nowworld[y]][U.leader(x)] > 0:
            print('Yes')
            worldleaderlist[nowworld[x]][U.leader(x)] -= 1
            nowworld[x] = nowworld[y]
            worldleaderlist[nowworld[x]][U.leader(x)] += 1
        else:
            print('No')
else:
    nowworld = [v - 1 for v in li()] #n人の今いるワールド

    worldlist = [set() for _ in range(n)]

    for i in range(n):
        worldlist[nowworld[i]].add(i)
    graph = [set() for _ in range(n)]

    for _ in range(m):
        u, v = mi()
        u -= 1; v -= 1
        graph[u].add(v)
        graph[v].add(u)

    def common(x, y):
        if len(graph[x]) < len(worldlist[nowworld[y]]):
            for v in graph[x]:
                if v in worldlist[nowworld[y]]:
                    return True
            return False
        else:
            for v in worldlist[nowworld[y]]:
                if v in graph[x]:
                    return True
            return False
    for _ in range(ii()):
        x, y = mi()
        x -= 1; y -= 1
        if nowworld[x] != nowworld[y] and common(x, y):
            worldlist[nowworld[x]].discard(x)
            nowworld[x] = nowworld[y]
            worldlist[nowworld[x]].add(x)
            print('Yes')
        else:
            print('No')
0