結果

問題 No.2319 Friends+
ユーザー lam6er
提出日時 2025-04-15 21:43:30
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,762 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 82,076 KB
実行使用メモリ 165,444 KB
最終ジャッジ日時 2025-04-15 21:45:18
合計ジャッジ時間 6,297 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other TLE * 1 -- * 44
権限があれば一括ダウンロードができます

ソースコード

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
    for i in range(N):
        current_world[i+1] = P[i]
    
    friends = [[] for _ in range(N+1)]
    for _ in range(M):
        A = int(input[ptr])
        B = int(input[ptr+1])
        ptr += 2
        friends[A].append(B)
        friends[B].append(A)
    
    # Initialize friends_in_world
    friends_in_world = [defaultdict(int) for _ in range(N+1)]  # 1-based
    for x in range(1, N+1):
        for f in friends[x]:
            w = current_world[f]
            friends_in_world[x][w] += 1
    
    Q = int(input[ptr])
    ptr += 1
    for _ in range(Q):
        X = int(input[ptr])
        Y = int(input[ptr+1])
        ptr += 2
        current_X = current_world[X]
        current_Y = current_world[Y]
        if current_X == current_Y:
            print("No")
            continue
        # Check if friends_in_world[X] has current_Y
        if friends_in_world[X].get(current_Y, 0) >= 1:
            print("Yes")
            # Move X to current_Y
            # Update all friends of X
            for f in friends[X]:
                # Decrement old world
                old_w = current_X
                friends_in_world[f][old_w] -= 1
                if friends_in_world[f][old_w] == 0:
                    del friends_in_world[f][old_w]
                # Increment new world
                new_w = current_Y
                friends_in_world[f][new_w] += 1
            current_world[X] = current_Y
        else:
            print("No")
    
if __name__ == "__main__":
    main()
0