結果

問題 No.1640 簡単な色塗り
ユーザー lam6er
提出日時 2025-03-31 17:46:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,040 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 82,108 KB
実行使用メモリ 111,752 KB
最終ジャッジ日時 2025-03-31 17:46:46
合計ジャッジ時間 11,649 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    n = int(input[idx])
    idx += 1
    pairs = []
    for _ in range(n):
        a = int(input[idx])
        b = int(input[idx + 1])
        pairs.append((a, b))
        idx += 2

    used = [False] * (n + 1)
    res = [0] * n

    # Process in reverse order (i from n-1 downto 0)
    for i in range(n-1, -1, -1):
        a, b = pairs[i]
        if not used[a] and not used[b]:
            # Choose the one that appears first; arbitrary choice to prefer a
            res[i] = a
            used[a] = True
        elif not used[a]:
            res[i] = a
            used[a] = True
        elif not used[b]:
            res[i] = b
            used[b] = True
        else:
            # Both are used, choose any (here a)
            res[i] = a

    # Check if all are used
    all_used = all(used[1:n+1])
    if not all_used:
        print("No")
    else:
        print("Yes")
        for num in res:
            print(num)

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