結果

問題 No.1640 簡単な色塗り
ユーザー harurunharurun
提出日時 2021-06-16 23:57:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 947 ms / 2,000 ms
コード長 4,172 bytes
コンパイル時間 84 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 71,548 KB
最終ジャッジ日時 2024-06-29 14:42:45
合計ジャッジ時間 25,003 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
11,520 KB
testcase_01 AC 28 ms
11,392 KB
testcase_02 AC 29 ms
11,264 KB
testcase_03 AC 27 ms
11,392 KB
testcase_04 AC 477 ms
66,732 KB
testcase_05 AC 560 ms
71,548 KB
testcase_06 AC 28 ms
11,264 KB
testcase_07 AC 27 ms
11,392 KB
testcase_08 AC 26 ms
11,392 KB
testcase_09 AC 27 ms
11,264 KB
testcase_10 AC 436 ms
46,652 KB
testcase_11 AC 368 ms
40,736 KB
testcase_12 AC 285 ms
37,452 KB
testcase_13 AC 800 ms
64,868 KB
testcase_14 AC 798 ms
64,668 KB
testcase_15 AC 213 ms
30,000 KB
testcase_16 AC 258 ms
34,108 KB
testcase_17 AC 618 ms
56,652 KB
testcase_18 AC 65 ms
15,360 KB
testcase_19 AC 303 ms
36,772 KB
testcase_20 AC 440 ms
45,540 KB
testcase_21 AC 369 ms
39,416 KB
testcase_22 AC 52 ms
13,952 KB
testcase_23 AC 484 ms
48,220 KB
testcase_24 AC 124 ms
21,628 KB
testcase_25 AC 298 ms
37,624 KB
testcase_26 AC 544 ms
51,984 KB
testcase_27 AC 209 ms
28,932 KB
testcase_28 AC 716 ms
62,196 KB
testcase_29 AC 495 ms
51,392 KB
testcase_30 AC 102 ms
19,968 KB
testcase_31 AC 863 ms
70,112 KB
testcase_32 AC 725 ms
62,848 KB
testcase_33 AC 412 ms
46,976 KB
testcase_34 AC 444 ms
55,184 KB
testcase_35 AC 315 ms
46,068 KB
testcase_36 AC 91 ms
19,952 KB
testcase_37 AC 108 ms
23,248 KB
testcase_38 AC 538 ms
65,048 KB
testcase_39 AC 250 ms
34,160 KB
testcase_40 AC 219 ms
34,640 KB
testcase_41 AC 542 ms
56,048 KB
testcase_42 AC 319 ms
38,064 KB
testcase_43 AC 261 ms
38,864 KB
testcase_44 AC 344 ms
39,012 KB
testcase_45 AC 184 ms
31,496 KB
testcase_46 AC 94 ms
20,992 KB
testcase_47 AC 75 ms
17,536 KB
testcase_48 AC 534 ms
66,016 KB
testcase_49 AC 48 ms
14,080 KB
testcase_50 AC 26 ms
11,392 KB
testcase_51 AC 27 ms
11,392 KB
testcase_52 AC 947 ms
71,292 KB
testcase_53 AC 929 ms
71,296 KB
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#assert
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)
  assert(1<=N<=10**5)
  uf=dsu(N)
  to=[[]for _ in[0]*N]
  for i in range(N):
    A,B=pin(2)
    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])
  is_input=False
  try:
    pin(0)
    is_input=True
  except:
    pass
  if is_input:
    raise Exception
  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