結果

問題 No.3093 Safe Infection
ユーザー titia
提出日時 2025-05-05 02:47:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 307 ms / 2,000 ms
コード長 1,293 bytes
コンパイル時間 844 ms
コンパイル使用メモリ 81,972 KB
実行使用メモリ 110,120 KB
最終ジャッジ日時 2025-05-05 02:47:36
合計ジャッジ時間 13,695 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 71
権限があれば一括ダウンロードができます

ソースコード

diff #

# 解説を見た。
# 小さいのを大きい方に合わせる方針でずっと上手くいかず困ったが、
# 大きいのを小さい方に合わせる方針じゃないとダメなのか?

import sys
input = sys.stdin.readline

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

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

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

# UnionFind

Group = [i for i in range(N)] # グループ分け
Nodes = [1]*(N) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)
            
SA=[(A[i],i) for i in range(N)]
SA.sort()

for a,ind in SA:
    fi=find(ind)

    LIST=[]
    MAX=A[fi]

    for to in E[fi]:
        if a>=A[find(to)] and a-A[find(to)]<=K:
            Union(ind,to)
            A[find(ind)]=a
            
if Nodes[find(0)]==N:
    print("Yes")
else:
    print("No")
0