結果

問題 No.3580 二成分の和
コンテスト
ユーザー ゼット
提出日時 2026-07-03 21:49:14
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,000 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 363 ms
コンパイル使用メモリ 85,376 KB
実行使用メモリ 81,792 KB
最終ジャッジ日時 2026-07-03 21:50:11
合計ジャッジ時間 3,933 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

N,M,B=map(int,input().split())
result=[-1]*N
G=[[] for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    G[a-1].append((b-1,c))
    G[b-1].append((a-1,c))
from collections import deque
kaku=[False]*N
for i in range(N):
    if kaku[i]==True:
        continue
    for x in range(B):
        S=deque()
        S.append(i)
        A=set()
        A.add(i)
        result[i]=x
        ans=True
        while S:
            pos=S.pop()
            for E in G[pos]:
                ne,t=E[:]
                if result[ne]==-1:
                    result[ne]=(t-result[pos])%B
                    S.append(ne)
                    A.add(ne)
                else:
                    if result[ne]!=(t-result[pos])%B:
                        ans=False
        if ans==True:
            break
        else:
            for pos in A:
                result[pos]=-1
                kaku[pos]=True
if min(result)>=0:
    print('Yes')
    print(*result)
else:
    print('No')
                
0