結果
問題 |
No.3093 Safe Infection
|
ユーザー |
|
提出日時 | 2025-04-06 22:14:57 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,225 bytes |
コンパイル時間 | 371 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 109,924 KB |
最終ジャッジ日時 | 2025-04-06 22:15:25 |
合計ジャッジ時間 | 24,751 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 67 WA * 2 TLE * 1 |
ソースコード
class UnionFind: def __init__(self, n, w=None): self.par = [-1]*n self.rank = [0]*n self.siz = [1]*n self.cnt = n self.min_node = [i for i in range(n)] self.weight = w def root(self, x): if self.par[x] == -1: return x self.par[x] = self.root(self.par[x]) return self.par[x] def issame(self, x, y): return self.root(x) == self.root(y) def unite(self, x, y): px = self.root(x) py = self.root(y) if px == py: return False if self.rank[px] < self.rank[py]: px, py = py, px self.par[py] = px if self.rank[px] == self.rank[py]: self.rank[px] += 1 self.siz[px] += self.siz[py] self.cnt -= 1 self.min_node[px] = min(self.min_node[px], self.min_node[py]) if len(self.weight[px])<len(self.weight[py]): for g in self.weight[px]: if self.root(g[1])==py: continue self.weight[py].append(g) self.weight[px] = self.weight[py] self.weight[py] = [] else: for g in self.weight[py]: if self.root(g[1])==px: continue self.weight[px].append(g) self.weight[py] = [] return False def count(self): return self.cnt def min(self, x): return self.min_node[self.root(x)] def getweight(self, x): return self.weight[self.root(x)] def size(self, x): return self.siz[self.root(x)] import heapq import sys input = sys.stdin.readline N, M, K = map(int, input().split()) A = list(map(int, input().split())) G = [[] for _ in range(N)] for _ in range(M): u, v = map(int, input().split()) u-=1 v-=1 G[u].append((A[v], v)) G[v].append((A[u], u)) UF = UnionFind(N, G) dp = [] for i, a in enumerate(A): dp.append((a, i)) dp.sort(reverse=True) while len(dp)>1: a, i = dp.pop() ri = UF.root(i) heapq.heapify(G[ri]) s, j = heapq.heappop(G[ri]) rj = UF.root(j) if s-a>K: print("No") exit() UF.unite(i, j) print("Yes")