結果

問題 No.2319 Friends+
ユーザー ShirotsumeShirotsume
提出日時 2023-05-26 22:54:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,165 ms / 3,000 ms
コード長 3,725 bytes
コンパイル時間 555 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 154,272 KB
最終ジャッジ日時 2024-06-07 08:28:20
合計ジャッジ時間 26,574 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,400 KB
testcase_01 AC 42 ms
54,272 KB
testcase_02 AC 504 ms
143,872 KB
testcase_03 AC 515 ms
143,872 KB
testcase_04 AC 507 ms
144,080 KB
testcase_05 AC 508 ms
144,100 KB
testcase_06 AC 510 ms
144,000 KB
testcase_07 AC 595 ms
147,224 KB
testcase_08 AC 584 ms
147,160 KB
testcase_09 AC 597 ms
147,520 KB
testcase_10 AC 594 ms
147,152 KB
testcase_11 AC 602 ms
147,020 KB
testcase_12 AC 58 ms
65,408 KB
testcase_13 AC 55 ms
65,664 KB
testcase_14 AC 55 ms
65,792 KB
testcase_15 AC 54 ms
65,536 KB
testcase_16 AC 56 ms
65,536 KB
testcase_17 AC 41 ms
54,400 KB
testcase_18 AC 2,165 ms
149,388 KB
testcase_19 AC 447 ms
150,376 KB
testcase_20 AC 592 ms
139,096 KB
testcase_21 AC 532 ms
139,692 KB
testcase_22 AC 648 ms
139,444 KB
testcase_23 AC 503 ms
141,844 KB
testcase_24 AC 502 ms
140,904 KB
testcase_25 AC 493 ms
140,820 KB
testcase_26 AC 483 ms
140,364 KB
testcase_27 AC 495 ms
140,220 KB
testcase_28 AC 491 ms
139,728 KB
testcase_29 AC 506 ms
139,540 KB
testcase_30 AC 602 ms
140,768 KB
testcase_31 AC 578 ms
139,764 KB
testcase_32 AC 528 ms
141,148 KB
testcase_33 AC 517 ms
139,744 KB
testcase_34 AC 502 ms
138,924 KB
testcase_35 AC 493 ms
140,336 KB
testcase_36 AC 454 ms
153,344 KB
testcase_37 AC 415 ms
153,216 KB
testcase_38 AC 484 ms
151,936 KB
testcase_39 AC 482 ms
152,064 KB
testcase_40 AC 463 ms
151,936 KB
testcase_41 AC 474 ms
152,704 KB
testcase_42 AC 472 ms
153,216 KB
testcase_43 AC 478 ms
153,376 KB
testcase_44 AC 473 ms
154,272 KB
testcase_45 AC 441 ms
146,548 KB
testcase_46 AC 421 ms
153,104 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()
P = li()
AB = [li() for _ in range(m)]
Q = [li() for _ in range(ii())]
nowworld = [v - 1 for v in P] #n人の今いるワールド

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

for i in range(n):
    worldlist[nowworld[i]].add(i)
graph = [set() for _ in range(n)]
ans = []
for u, v in AB:
    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
ans = []
import time
t1 = time.time()
for i in range(len(Q)):
    x, y = Q[i]
    x -= 1; y -= 1
    if time.time() - t1 >= 1.7:
        ans = []
        break
    if nowworld[x] != nowworld[y] and common(x, y):
        worldlist[nowworld[x]].discard(x)
        nowworld[x] = nowworld[y]
        worldlist[nowworld[x]].add(x)
        ans.append('Yes')
    else:
        ans.append('No')
else:
    for v in ans:
        print(v)
    exit()
nowworld = [v - 1 for v in P] #n人の今いるワールド

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

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

for _ in range(len(Q)):
    x, y = Q[i]
    x -= 1; y -= 1
    if nowworld[x] != nowworld[y] and worldleaderlist[nowworld[y]][U.leader(x)] > 0:
        ans.append('Yes')
        worldleaderlist[nowworld[x]][U.leader(x)] -= 1
        nowworld[x] = nowworld[y]
        worldleaderlist[nowworld[x]][U.leader(x)] += 1
    else:
        ans.append('No')

for v in ans:
    print(v)
0