結果

問題 No.2319 Friends+
ユーザー lam6er
提出日時 2025-04-15 21:42:11
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,277 bytes
コンパイル時間 603 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 142,924 KB
最終ジャッジ日時 2025-04-15 21:44:11
合計ジャッジ時間 11,588 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 16 TLE * 1 -- * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    input = sys.stdin.read().split()
    ptr = 0
    n, m = int(input[ptr]), int(input[ptr+1])
    ptr += 2
    p = list(map(int, input[ptr:ptr+n]))
    ptr += n
    current_world = [0] * (n + 1)  # 1-based indexing
    for i in range(n):
        current_world[i+1] = p[i]
    inhabitants = defaultdict(set)
    for i in range(1, n+1):
        inhabitants[current_world[i]].add(i)
    friends = [set() for _ in range(n+1)]
    for _ in range(m):
        a = int(input[ptr])
        b = int(input[ptr+1])
        ptr += 2
        friends[a].add(b)
        friends[b].add(a)
    q = int(input[ptr])
    ptr += 1
    for _ in range(q):
        x = int(input[ptr])
        y = int(input[ptr+1])
        ptr += 2
        x_w = current_world[x]
        y_w = current_world[y]
        if x_w == y_w:
            print("No")
            continue
        # Check if friends[x] and inhabitants[y_w] have any common element
        if not friends[x].isdisjoint(inhabitants[y_w]):
            print("Yes")
            # Move x from x_w to y_w
            inhabitants[x_w].remove(x)
            inhabitants[y_w].add(x)
            current_world[x] = y_w
        else:
            print("No")

if __name__ == "__main__":
    main()
0