結果

問題 No.1640 簡単な色塗り
ユーザー harurunharurun
提出日時 2021-06-17 13:52:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,123 ms / 2,000 ms
コード長 2,754 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 60,452 KB
最終ジャッジ日時 2024-06-29 14:45:47
合計ジャッジ時間 28,734 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,264 KB
testcase_01 AC 32 ms
11,264 KB
testcase_02 AC 32 ms
11,136 KB
testcase_03 AC 31 ms
11,264 KB
testcase_04 AC 490 ms
55,748 KB
testcase_05 AC 589 ms
60,212 KB
testcase_06 AC 31 ms
11,264 KB
testcase_07 AC 31 ms
11,264 KB
testcase_08 AC 32 ms
11,264 KB
testcase_09 AC 31 ms
11,264 KB
testcase_10 AC 558 ms
40,396 KB
testcase_11 AC 461 ms
34,824 KB
testcase_12 AC 371 ms
32,600 KB
testcase_13 AC 930 ms
55,080 KB
testcase_14 AC 945 ms
54,780 KB
testcase_15 AC 276 ms
26,580 KB
testcase_16 AC 328 ms
29,668 KB
testcase_17 AC 771 ms
47,480 KB
testcase_18 AC 69 ms
14,464 KB
testcase_19 AC 365 ms
31,936 KB
testcase_20 AC 551 ms
39,212 KB
testcase_21 AC 439 ms
33,928 KB
testcase_22 AC 57 ms
13,184 KB
testcase_23 AC 581 ms
41,364 KB
testcase_24 AC 139 ms
19,456 KB
testcase_25 AC 363 ms
32,768 KB
testcase_26 AC 651 ms
45,312 KB
testcase_27 AC 248 ms
25,820 KB
testcase_28 AC 890 ms
52,816 KB
testcase_29 AC 615 ms
43,976 KB
testcase_30 AC 114 ms
18,432 KB
testcase_31 AC 1,008 ms
59,296 KB
testcase_32 AC 841 ms
53,212 KB
testcase_33 AC 514 ms
40,100 KB
testcase_34 AC 518 ms
46,952 KB
testcase_35 AC 373 ms
39,700 KB
testcase_36 AC 99 ms
18,176 KB
testcase_37 AC 120 ms
20,864 KB
testcase_38 AC 638 ms
55,016 KB
testcase_39 AC 308 ms
30,064 KB
testcase_40 AC 257 ms
30,668 KB
testcase_41 AC 655 ms
48,140 KB
testcase_42 AC 382 ms
33,140 KB
testcase_43 AC 298 ms
33,516 KB
testcase_44 AC 389 ms
33,476 KB
testcase_45 AC 196 ms
27,980 KB
testcase_46 AC 104 ms
19,072 KB
testcase_47 AC 82 ms
16,128 KB
testcase_48 AC 627 ms
56,064 KB
testcase_49 AC 52 ms
13,312 KB
testcase_50 AC 31 ms
11,264 KB
testcase_51 AC 31 ms
11,136 KB
testcase_52 AC 1,105 ms
60,400 KB
testcase_53 AC 1,123 ms
60,452 KB
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#行数assert
class dsu:
  def __init__(self,n):
    assert n>0,"n must be Natural"
    self.n=n
    self.parents=[-1]*n
    return
  
  def find(self,x):
    assert x<self.n,"warning!! this is 0 index"
    if self.parents[x]<0:
      return x
    self.parents[x]=self.find(self.parents[x])
    return self.parents[x]
  
  def merge(self,x,y):
    assert x<self.n and y<self.n,"warning!! this is 0 index"
    x=self.find(x)
    y=self.find(y)
    if x==y:
      return
    if self.parents[x]>self.parents[y]:
      x,y=y,x
    self.parents[x]+=self.parents[y]
    self.parents[y]=x
    return
    
  def size(self,x):
    assert x<self.n,"warning!! this is 0 index"
    return -self.parents[self.find(x)]
    
  def same(self,x,y):
    assert x<self.n and y<self.n,"warning!! this is 0 index"
    return self.find(x)==self.find(y)
  
  def leader(self,x):
    assert x<self.n,"warning!! this is 0 index"
    if self.parents[x]<0:
      return x
    self.parents[x]=self.leader(self.parents[x])
    return self.parents[x]
  
  def groups(self):
    ld=[self.leader(i) for i in range(self.n)]
    res=[[] for _ in range(self.n)]
    for i in range(self.n):
      res[ld[i]].append(i)
    return list(filter(lambda a:a !=[],res))

from sys import stdin
pin=stdin.readline
from collections import deque
def main():
  N=int(pin())
  assert(1<=N<=10**5)
  uf=dsu(N)
  to=[[]for _ in[0]*N]
  for i in range(N):
    A,B=map(int,pin().split())
    assert(1<=A<=N)
    assert(1<=B<=N)
    uf.merge(A-1,B-1)
    to[A-1].append([B-1,i])
    if A!=B:
      to[B-1].append([A-1,i])
  seen=[0]*N
  ans=[-1]*N
  
  gr=uf.groups()
  for linked in gr:
    is_cycle=True
    root=-1
    cycle_cnt=0
    
    for point in linked:
      link_size=len(to[point])
      for k in range(link_size):
        if to[point][k][0]==point:
          cycle_cnt+=1
          is_cycle=False
          ans[to[point][k][1]]=point+1
          root=point
    
    if cycle_cnt>1:
      print("No")
      return
    
    if is_cycle:
      next_point=-1
      que=deque()
      que.append([linked[0],-1])
      
      while que:
        q=que.pop()
        for j in to[q[0]]:
          if j[1]==q[1]:
            continue
          if seen[j[0]]:
            root=j[0]
            next_point=q[0]
            ans[j[1]]=root+1
            que.clear()
            break
          que.append([j[0],j[1]])
        seen[q[0]]=1
      
      if next_point==-1:
        print("No")
        return
      
    dq=deque()
    dq.append(root)

    while dq:
      q=dq.pop()

      for j in to[q]:
        if ans[j[1]]==-1:
          ans[j[1]]=j[0]+1
          dq.append(j[0])
  
  for i in ans:
    if i==-1:
      print("No")
      return
  
  print("Yes")
  for i in ans:
    print(i)
  return

main()
0