結果

問題 No.1640 簡単な色塗り
ユーザー qibqib
提出日時 2023-03-10 02:45:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 1,272 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 81,612 KB
実行使用メモリ 98,556 KB
最終ジャッジ日時 2023-10-18 06:36:34
合計ジャッジ時間 18,692 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,420 KB
testcase_01 AC 41 ms
55,420 KB
testcase_02 AC 42 ms
55,420 KB
testcase_03 AC 41 ms
55,420 KB
testcase_04 AC 152 ms
86,344 KB
testcase_05 AC 188 ms
98,028 KB
testcase_06 AC 41 ms
55,420 KB
testcase_07 AC 41 ms
55,420 KB
testcase_08 AC 41 ms
55,420 KB
testcase_09 AC 42 ms
55,420 KB
testcase_10 AC 232 ms
90,048 KB
testcase_11 AC 197 ms
87,056 KB
testcase_12 AC 185 ms
86,100 KB
testcase_13 AC 312 ms
96,888 KB
testcase_14 AC 302 ms
96,884 KB
testcase_15 AC 170 ms
83,452 KB
testcase_16 AC 178 ms
85,380 KB
testcase_17 AC 265 ms
92,628 KB
testcase_18 AC 116 ms
77,984 KB
testcase_19 AC 194 ms
86,028 KB
testcase_20 AC 231 ms
89,768 KB
testcase_21 AC 198 ms
86,688 KB
testcase_22 AC 110 ms
77,268 KB
testcase_23 AC 243 ms
90,596 KB
testcase_24 AC 138 ms
80,476 KB
testcase_25 AC 203 ms
86,192 KB
testcase_26 AC 264 ms
91,436 KB
testcase_27 AC 161 ms
82,668 KB
testcase_28 AC 312 ms
96,588 KB
testcase_29 AC 256 ms
91,432 KB
testcase_30 AC 95 ms
77,732 KB
testcase_31 AC 237 ms
93,192 KB
testcase_32 AC 203 ms
89,736 KB
testcase_33 AC 161 ms
86,760 KB
testcase_34 AC 191 ms
88,516 KB
testcase_35 AC 144 ms
84,388 KB
testcase_36 AC 93 ms
77,680 KB
testcase_37 AC 98 ms
78,224 KB
testcase_38 AC 207 ms
92,124 KB
testcase_39 AC 130 ms
82,636 KB
testcase_40 AC 129 ms
82,736 KB
testcase_41 AC 180 ms
88,600 KB
testcase_42 AC 131 ms
83,268 KB
testcase_43 AC 140 ms
83,416 KB
testcase_44 AC 137 ms
83,308 KB
testcase_45 AC 124 ms
82,224 KB
testcase_46 AC 99 ms
77,852 KB
testcase_47 AC 93 ms
77,408 KB
testcase_48 AC 232 ms
92,368 KB
testcase_49 AC 91 ms
76,952 KB
testcase_50 AC 42 ms
55,420 KB
testcase_51 AC 43 ms
55,420 KB
testcase_52 AC 341 ms
98,556 KB
testcase_53 AC 330 ms
98,556 KB
07_evil_01.txt AC 681 ms
122,568 KB
07_evil_02.txt AC 1,002 ms
146,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n = int(input())

g = [[] for _ in range(n)]
deg = [0 for _ in range(n)]
used = [False for _ in range(n)]
ans = [None for _ in range(n)]
for e in range(n):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  if a == b:
    used[a] = True
    ans[e] = a + 1
  else:
    g[a].append((b, e))
    g[b].append((a, e))
    deg[a] += 1
    deg[b] += 1

dq = deque()
for u in range(n):
  if used[u]:
    continue
  if deg[u] == 0:
    print("No")
    exit()
  elif deg[u] == 1:
    used[u] = True
    dq.append(u)

while len(dq) > 0:
  cur = dq.popleft()
  for nxt, e in g[cur]:
    if not ans[e] is None:
      continue
    deg[nxt] -= 1
    ans[e] = cur + 1
    if used[nxt]:
      continue

    if deg[nxt] == 0:
      print("No")
      exit()
    elif deg[nxt] == 1:
      used[nxt] = True
      dq.append(nxt)

for src in range(n):
  if used[src]:
    continue
  dq.append(src)
  used[src] = True
  while len(dq) > 0:
    cur = dq.popleft()
    while len(g[cur]) > 0:
      nxt, e = g[cur].pop()
      if not ans[e] is None:
        continue
      ans[e] = cur + 1
      if used[nxt]:
        continue
      used[nxt] = True
      dq.append(nxt)
      break

if ans.count(None) > 0:
  print("No")
else:
  print("Yes")
  print(*ans, sep="\n")
0