結果
問題 | No.2642 Don't cut line! |
ユーザー |
![]() |
提出日時 | 2024-02-20 01:14:07 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,340 bytes |
コンパイル時間 | 529 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 374,116 KB |
最終ジャッジ日時 | 2024-09-29 03:20:56 |
合計ジャッジ時間 | 6,134 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 3 |
other | RE * 1 TLE * 1 -- * 31 |
ソースコード
class UF:def __init__(self,n):self.p = [-1]*ndef f(self,x):if self.p[x]<0: return xelse: self.p[x] = self.f(self.p[x]); return self.p[x]def u(self,x,y):x = self.f(x); y = self.f(y)if x==y: returnif -self.p[x]<-self.p[y]: x,y = y,xself.p[x] += self.p[y]; self.p[y] = xdef s(self,x,y): return self.f(x) == self.f(y)class DoublingLCA:def __init__(self,n,adj,root=0):self.ancestor,self.depth,self.weight = [[-1]*(n+1)],[-1]*n,[[0]*(n+1)]stack = [(root,-1,0)]while stack:v,p,dep = stack.pop()self.ancestor[0][v],self.depth[v] = p,depfor c,w in adj[v]:if c!=p:stack.append((c,v,dep+1))self.weight[0][c] = max(self.weight[0][c],w)for _ in range(max(self.depth).bit_length()-1):next_ancestor = []; next_weight = []for v in self.ancestor[-1]:u = self.ancestor[-1][v]next_ancestor.append(u)next_weight.append(max(self.weight[-1][u],self.weight[-1][v]))self.ancestor.append(next_ancestor)self.weight.append(next_weight)def get_kth_ancestor(self,v,k):w = 0for i in range(k.bit_length()):if k>>i&1:v = self.ancestor[i][v]w = max(w,self.weight[i][v])return v,wdef get_max_weight(self,u,v):depu,depv = self.depth[u],self.depth[v]if depu>depv: u,v,depu,depv = v,u,depv,depuv,w = self.get_kth_ancestor(v,depv-depu)if u==v: return wfor k in range(depu.bit_length()-1,-1,-1):nu,nv = self.ancestor[k][u],self.ancestor[k][v]w = max(w,self.weight[k][u],self.weight[k][v])if nu!=nv: u,v = nu,nvreturn wn,k,c = map(int,input().split())wpuv = []for _ in range(k):u,v,w,p = map(int,input().split())wpuv.append((w,p,u-1,v-1))uf = UF(n); d = m = 0; adj = [[] for _ in range(n)]for w,p,u,v in sorted(wpuv):if uf.s(u,v): continueuf.u(u,v); d += w; m = max(m,p)adj[u-1].append((v,w)); adj[v-1].append((u,w))if d>c: exit(print(-1))lca = DoublingLCA(n,adj)for w,p,u,v in wpuv:if d-lca.get_max_weight(u,v)+w<=c: m = max(m,p)print(m)