結果

問題 No.2319 Friends+
ユーザー lam6er
提出日時 2025-04-16 15:31:07
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,673 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 162,008 KB
最終ジャッジ日時 2025-04-16 15:36:15
合計ジャッジ時間 6,134 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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

    current_world = [0] * (N + 1)  # 1-based
    for i in range(1, N+1):
        current_world[i] = int(input[ptr])
        ptr += 1

    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)]
    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

        if current_world[X] == current_world[Y]:
            print("No")
            continue

        target_world = current_world[Y]
        cnt = friends_in_world[X].get(target_world, 0)
        if cnt >= 1:
            print("Yes")
            old_world = current_world[X]
            current_world[X] = target_world

            for F in friends[X]:
                # Update F's friends_in_world for old_world
                if friends_in_world[F][old_world] == 1:
                    del friends_in_world[F][old_world]
                else:
                    friends_in_world[F][old_world] -= 1

                # Update F's friends_in_world for new_world (target_world)
                friends_in_world[F][target_world] += 1
        else:
            print("No")

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