結果

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

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

N,M,mod=list(map(int,input().split()))

E=[[] for i in range(N)]

for i in range(M):
    x,y,c=list(map(int,input().split()))
    x-=1
    y-=1
    E[x].append((y,c))
    E[y].append((x,c))


ANS=[-1]*N

for i in range(N):
    if ANS[i]!=-1:
        continue

    else:
        for j in range(mod):
            Q=[i]
            ANS[i]=j
            LIST=[]
            flag=1

            while Q:
                #print(Q,ANS,LIST)

                x=Q.pop()
                LIST.append(x)

                for to,c in E[x]:
                    tov=(c-ANS[x])%mod

                    if ANS[to]==-1:
                        ANS[to]=tov
                        Q.append(to)
                    else:
                        if ANS[to]!=tov:
                            flag=0
                            break

            #print(LIST)

            if flag:
                break
            else:
                for xx in LIST:
                    ANS[xx]=-1

        else:
            print("No")
            exit()

print("Yes")
print(*ANS)
                    
            
        

0