結果

問題 No.1640 簡単な色塗り
ユーザー harurunharurun
提出日時 2021-06-12 20:15:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 644 ms / 2,000 ms
コード長 3,995 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 82,660 KB
実行使用メモリ 127,084 KB
最終ジャッジ日時 2024-06-29 14:37:55
合計ジャッジ時間 27,563 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,156 KB
testcase_01 AC 42 ms
54,880 KB
testcase_02 AC 44 ms
55,520 KB
testcase_03 AC 43 ms
55,924 KB
testcase_04 AC 212 ms
123,636 KB
testcase_05 AC 233 ms
122,036 KB
testcase_06 AC 43 ms
55,320 KB
testcase_07 AC 44 ms
56,184 KB
testcase_08 AC 44 ms
56,092 KB
testcase_09 AC 43 ms
55,928 KB
testcase_10 AC 409 ms
105,788 KB
testcase_11 AC 351 ms
98,524 KB
testcase_12 AC 336 ms
99,008 KB
testcase_13 AC 546 ms
118,384 KB
testcase_14 AC 554 ms
118,936 KB
testcase_15 AC 269 ms
92,364 KB
testcase_16 AC 306 ms
94,708 KB
testcase_17 AC 483 ms
113,204 KB
testcase_18 AC 160 ms
80,564 KB
testcase_19 AC 324 ms
97,284 KB
testcase_20 AC 377 ms
102,408 KB
testcase_21 AC 342 ms
99,236 KB
testcase_22 AC 135 ms
79,440 KB
testcase_23 AC 416 ms
106,276 KB
testcase_24 AC 215 ms
85,720 KB
testcase_25 AC 329 ms
97,232 KB
testcase_26 AC 434 ms
106,924 KB
testcase_27 AC 272 ms
91,332 KB
testcase_28 AC 534 ms
117,416 KB
testcase_29 AC 418 ms
108,820 KB
testcase_30 AC 205 ms
83,892 KB
testcase_31 AC 559 ms
122,740 KB
testcase_32 AC 513 ms
118,924 KB
testcase_33 AC 369 ms
103,316 KB
testcase_34 AC 383 ms
111,116 KB
testcase_35 AC 318 ms
102,928 KB
testcase_36 AC 201 ms
83,972 KB
testcase_37 AC 199 ms
85,784 KB
testcase_38 AC 438 ms
117,640 KB
testcase_39 AC 309 ms
93,980 KB
testcase_40 AC 290 ms
94,496 KB
testcase_41 AC 442 ms
112,668 KB
testcase_42 AC 336 ms
96,704 KB
testcase_43 AC 311 ms
98,612 KB
testcase_44 AC 326 ms
97,772 KB
testcase_45 AC 272 ms
93,848 KB
testcase_46 AC 199 ms
84,512 KB
testcase_47 AC 183 ms
82,408 KB
testcase_48 AC 449 ms
119,800 KB
testcase_49 AC 137 ms
79,064 KB
testcase_50 AC 43 ms
54,860 KB
testcase_51 AC 43 ms
54,432 KB
testcase_52 AC 616 ms
126,324 KB
testcase_53 AC 644 ms
127,084 KB
07_evil_01.txt AC 1,059 ms
172,580 KB
07_evil_02.txt AC 1,481 ms
226,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class INPUT:
  def __init__(self):
    self._l=open(0).read().split()
    self._length=len(self._l)
    self._index=0
    return

  def stream(self,k=1,f=int,f2=False):
    assert(-1<k)
    if self._length==self._index or self._length-self._index<k:
      raise Exception("There is no input!")
    elif f!=str:
      if k==0:
        ret=list(map(f,self._l[self._index:]))
        self._index=self._length
        return ret
      if k==1 and not f2:
        ret=f(self._l[self._index])
        self._index+=1
        return ret
      if k==1 and f2:
        ret=[f(self._l[self._index])]
        self._index+=1
        return ret
      ret=[]
      for _ in [0]*k:
        ret.append(f(self._l[self._index]))
        self._index+=1
      return ret
    else:
      if k==0:
        ret=list(self._l[self._index:])
        self._index=self._length
        return ret
      if k==1 and not f2:
        ret=self._l[self._index]
        self._index+=1
        return ret
      if k==1 and f2:
        ret=[self._l[self._index]]
        self._index+=1
        return ret
      ret=[]
      for _ in [0]*k:
        ret.append(self._l[self._index])
        self._index+=1
      return ret
pin=INPUT().stream

"""
pin(number[default:1],f[default:int],f2[default:False])
if number==0 -> return left all
listを変数で受け取るとき、必ずlistをTrueにすること。
"""

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 collections import deque
def main():
  N=pin(1)
  uf=dsu(N)
  to=[[]for _ in[0]*N]
  for i in range(N):
    A,B=pin(2)
    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