結果
問題 |
No.2319 Friends+
|
ユーザー |
![]() |
提出日時 | 2025-04-15 21:36:25 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,110 bytes |
コンパイル時間 | 624 ms |
コンパイル使用メモリ | 82,204 KB |
実行使用メモリ | 172,612 KB |
最終ジャッジ日時 | 2025-04-15 21:38:12 |
合計ジャッジ時間 | 6,686 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | TLE * 1 -- * 44 |
ソースコード
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(1, N+1): current_world[i] = P[i-1] friends = [[] for _ in range(N+1)] # friends[1], ..., friends[N] for _ in range(M): A = int(input[ptr]) B = int(input[ptr+1]) ptr +=2 friends[A].append(B) friends[B].append(A) # Initialize freq_maps and friend_worlds freq_maps = [defaultdict(int) for _ in range(N+1)] friend_worlds = [set() for _ in range(N+1)] for X in range(1, N+1): for F in friends[X]: w = current_world[F] freq_maps[X][w] += 1 for w, cnt in freq_maps[X].items(): if cnt > 0: friend_worlds[X].add(w) 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 WY = current_world[Y] if WY in friend_worlds[X]: print("Yes") W_old = current_world[X] W_new = WY current_world[X] = W_new # Update all friends of X for F in friends[X]: # Decrement W_old in F's freq_map old_count = freq_maps[F].get(W_old, 0) if old_count == 1: friend_worlds[F].discard(W_old) if old_count > 0: freq_maps[F][W_old] -= 1 if freq_maps[F][W_old] == 0: del freq_maps[F][W_old] # Increment W_new in F's freq_map new_count = freq_maps[F].get(W_new, 0) if new_count == 0: friend_worlds[F].add(W_new) freq_maps[F][W_new] = new_count + 1 else: print("No") if __name__ == "__main__": main()