結果

問題 No.1436 Rgaph
ユーザー tyawanmusityawanmusi
提出日時 2020-10-31 20:57:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,580 bytes
コンパイル時間 84 ms
コンパイル使用メモリ 11,072 KB
実行使用メモリ 16,996 KB
最終ジャッジ日時 2023-09-29 20:36:29
合計ジャッジ時間 8,790 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,464 KB
testcase_01 AC 16 ms
8,396 KB
testcase_02 WA -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 17 ms
8,400 KB
testcase_06 RE -
testcase_07 AC 16 ms
8,396 KB
testcase_08 WA -
testcase_09 AC 16 ms
8,464 KB
testcase_10 RE -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 50 ms
16,756 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 53 ms
16,500 KB
testcase_18 AC 52 ms
16,580 KB
testcase_19 AC 50 ms
16,484 KB
testcase_20 AC 70 ms
11,712 KB
testcase_21 AC 68 ms
10,808 KB
testcase_22 AC 154 ms
14,088 KB
testcase_23 AC 295 ms
16,620 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 261 ms
16,756 KB
testcase_28 RE -
testcase_29 AC 630 ms
16,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def scc(n, edge):
  redge=[[]for _ in range(n)]
  for i in range(n):
    for j in edge[i]:
      redge[j].append(i)
  order=[]
  used=[0]*n
  group=[-1]*n
  def dfs(u):
    used[u]=1
    for v in edge[u]:
      if not used[u]:
        dfs(v)
    order.append(u)
  def rdfs(u,c):
    group[u]=c
    used[u]=1
    for v in redge[u]:
      if not used[v]:
        rdfs(v,c)
  for i in range(n):
    if not used[i]:
      dfs(i)
  used=[0]*n
  x=0
  for u in order:
    if not used[u]:
      rdfs(u,x)
      x+=1
  return x,group

n,m=map(int,input().split())
edge=[[] for _ in range(n)]
edges=[[-1]*n for _ in range(n)]
for i in range(m):
  a,b=map(int,input().split())
  a-=1
  b-=1
  edge[a].append(b)
  edges[a][b]=i

label,_=scc(n,edge)
if label!=1:
  exit(print(-1))
x=[]
for i in range(n):
  used=[0]*n
  co=[0]*n
  stack=[i]
  flag=False
  for u in stack:
    for v in edge[u]:
      if used[v]:continue
      used[v]=1
      co[v]=co[u]+1
      if i==v:
        flag=True
        break
      stack.append(v)
    if flag:
      break
  x.append(co[i])
s=x.index(min(x))
stack=[s]
root=[-1]*n
for u in stack:
  for v in edge[u]:
    if s==v:
      x=[u]
      while root[u]!=-1:
        u=root[u]
        x.append(u)
      ans=["R"]*m
      x=x[::-1]
      if len(x)==n:
        x.append(x[0])
        for i in range(n):
          if i%2==0:
            ans[edge[x[i]][x[i+1]]]="G"
      else:
        for i in range(len(x)//2+1):
          ans[edges[x[i]][x[i+1]]]="G"
      print("".join(ans))
      exit()
    if root[v]!=-1:
      continue
    root[v]=u
    stack.append(v)
0