結果

問題 No.1420 国勢調査 (Easy)
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-03-15 09:45:25
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,415 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 272,348 KB
最終ジャッジ日時 2024-04-24 14:30:56
合計ジャッジ時間 9,136 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
57,856 KB
testcase_01 AC 40 ms
52,480 KB
testcase_02 AC 594 ms
113,808 KB
testcase_03 AC 590 ms
113,424 KB
testcase_04 AC 677 ms
113,932 KB
testcase_05 AC 511 ms
113,296 KB
testcase_06 AC 614 ms
113,424 KB
testcase_07 AC 307 ms
110,480 KB
testcase_08 AC 263 ms
108,564 KB
testcase_09 AC 226 ms
107,148 KB
testcase_10 AC 239 ms
107,144 KB
testcase_11 AC 240 ms
107,020 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
def main1(n,m,aby):
  g=[[] for _ in range(n)]
  for a,b,y in aby:
    a,b=a-1,b-1
    g[a].append([b,y])
    g[b].append([a,y])
  x=[0]*n
  for i in range(30):
    bit=1<<i
    mi=set(range(n))
    while mi:
      v0=mi.pop()
      grp=set()
      flg=True
      for d in (0,bit):
        flg=True
        seen=[-1]*n
        seen[v0]=d
        todo=[[v0,-1]]
        while todo:
          v,p=todo.pop()
          mi.discard(v)
          grp.add(v)
          for nv,y in g[v]:
            if nv==p:continue
            elif seen[nv]==-1:
              seen[nv]=seen[v]^(y&bit)
              todo.append([nv,v])
            elif seen[nv]==seen[v]^(y&bit):
              continue
            else:
              flg=False
              break
          if not flg:break #うまくいかなかったのでwhileループを抜け、次のforに移る。
        if flg:break # うまくいくケースが見つかったのでforを出る。
      for v in grp:
        if seen[v]>0:
          x[v]+=bit
      if not flg:return [-1]
  return x

def chk(n,aby,x):
  for a,b,y in aby:
    a,b=a-1,b-1
    if x[a]^x[b]==y:
      pass
    else:
      return False
  return True

if __name__=='__main__':
  n,m=map(int,input().split())
  aby=[]
  for _ in range(m):
    a,b=map(int,input().split())
    y=int(input())
    aby.append([a,b,y])
  ret1=main1(n,m,aby)
  print(*ret1,sep='\n')
0