結果

問題 No.1420 国勢調査 (Easy)
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-03-15 10:21:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 1,056 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 133,264 KB
最終ジャッジ日時 2024-04-24 14:53:11
合計ジャッジ時間 9,955 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
51,968 KB
testcase_01 AC 41 ms
52,096 KB
testcase_02 AC 216 ms
99,728 KB
testcase_03 AC 252 ms
107,668 KB
testcase_04 AC 248 ms
107,924 KB
testcase_05 AC 255 ms
107,412 KB
testcase_06 AC 249 ms
107,920 KB
testcase_07 AC 228 ms
107,160 KB
testcase_08 AC 206 ms
98,840 KB
testcase_09 AC 236 ms
106,764 KB
testcase_10 AC 246 ms
106,648 KB
testcase_11 AC 237 ms
107,156 KB
testcase_12 AC 154 ms
100,224 KB
testcase_13 AC 177 ms
100,480 KB
testcase_14 AC 149 ms
100,224 KB
testcase_15 AC 172 ms
100,352 KB
testcase_16 AC 177 ms
100,352 KB
testcase_17 AC 174 ms
100,736 KB
testcase_18 AC 179 ms
100,864 KB
testcase_19 AC 173 ms
100,352 KB
testcase_20 AC 173 ms
100,096 KB
testcase_21 AC 187 ms
100,864 KB
testcase_22 AC 398 ms
133,244 KB
testcase_23 AC 389 ms
129,680 KB
testcase_24 AC 370 ms
133,264 KB
testcase_25 AC 370 ms
133,236 KB
testcase_26 AC 376 ms
129,424 KB
testcase_27 AC 213 ms
116,080 KB
testcase_28 AC 207 ms
116,208 KB
testcase_29 AC 215 ms
115,828 KB
testcase_30 AC 215 ms
115,700 KB
testcase_31 AC 216 ms
116,340 KB
権限があれば一括ダウンロードができます

ソースコード

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
  flg=True

  mi=set(range(n))
  seen=[-1]*n
  while mi:
    v0=mi.pop()
    grp=set()
    flg=True
    seen[v0]=0
    todo=[v0]
    grp={v0}
    while todo:
      v=todo.pop()
      mi.discard(v)
      grp.add(v)
      for nv,y in g[v]:
        if seen[nv]==-1:
          seen[nv]=seen[v]^y
          todo.append(nv)
        elif seen[nv]==seen[v]^y:
          continue
        else:
          flg=False
          break
      if not flg:break
    if not flg:return [-1]
    for v in grp:
      if seen[v]>0:
        x[v]+=seen[v]
  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