結果
問題 | No.2642 Don't cut line! |
ユーザー |
![]() |
提出日時 | 2024-02-23 16:03:49 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 837 ms / 4,000 ms |
コード長 | 3,550 bytes |
コンパイル時間 | 211 ms |
コンパイル使用メモリ | 82,600 KB |
実行使用メモリ | 158,428 KB |
最終ジャッジ日時 | 2024-09-29 05:11:08 |
合計ジャッジ時間 | 23,357 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
N,K,C=map(int,input().split())par=[i for i in range(N)]rank=[0]*(N)friend=[0]*Nblock=[0]*Nsize=[1]*Ndef find(x):if par[x]==x:return xelse:par[x]=find(par[x])return par[x]#同じ集合か判定def same(x,y):return find(x)==find(y)def union(x,y):x=find(x)y=find(y)if x==y:returnif rank[x]>rank[y]:par[y]=xsize[x]+=size[y]else:par[x]=ysize[y]+=size[x]if rank[x]==rank[y]:rank[y]+=1from collections import dequeclass LowestCommonAncestor:def __init__(self, n):self._n = nself._logn = 0v = 1while v <= self._n:v *= 2self._logn += 1self._depth = [0] * self._nself._distance = [0] * self._nself._ancestor = [[-1] * self._n for _ in range(self._logn)]self.dp = [[-1] * self._n for _ in range(self._logn)]self._G = [[] for i in range(self._n)]def add_edge(self, u, v, w=1):self._G[u].append((v, w))self._G[v].append((u, w))def build(self, root=0):stack = [root]while stack:cur = stack.pop()for nex, w in self._G[cur]:if self._ancestor[0][nex] != cur and self._ancestor[0][cur] != nex:self._ancestor[0][nex] = curself._depth[nex] = self._depth[cur] + 1self._distance[nex] = self._distance[cur] + wstack.append(nex)self.dp[0][nex] = wfor k in range(1, self._logn):for i in range(self._n):if self._ancestor[k - 1][i] == -1:self._ancestor[k][i] = -1self.dp[k][i] = self.dp[k-1][i]else:self._ancestor[k][i] = self._ancestor[k - 1][self._ancestor[k - 1][i]]self.dp[k][i] = max(self.dp[k-1][i], self.dp[k-1][self._ancestor[k - 1][i]])def lca(self, u, v):if self._depth[u] > self._depth[v]:u, v = v, ufor k in range(self._logn - 1, -1, -1):if ((self._depth[v] - self._depth[u]) >> k) & 1:v = self._ancestor[k][v]if u == v:return ufor k in range(self._logn - 1, -1, -1):if self._ancestor[k][u] != self._ancestor[k][v]:u = self._ancestor[k][u]v = self._ancestor[k][v]return self._ancestor[0][u]def distance(self, u, v):return self._distance[u] + self._distance[v] - 2 * self._distance[self.lca(u, v)]def query(self, a, b):lca = self.lca(a, b)return max(self.max_edge(a, self._depth[a] - self._depth[lca]), self.max_edge(b, self._depth[b] - self._depth[lca]))def max_edge(self, a, d):now = -10 ** 18for i in range(self._logn):if (d >> i) & 1:now = max(now, self.dp[i][a])a = self._ancestor[i][a]return nowA=[]for i in range(K):u,v,w,p=map(int,input().split())u-=1;v-=1A.append((w,u,v,p))A=sorted(A)B=[];AA=[]T = LowestCommonAncestor(N)cost=0;pp=0for w,u,v,p in A:if not same(u,v):union(u,v)AA.append((u,v,w))T.add_edge(u,v,w)pp=max(p,pp)cost+=welse:B.append((p,u,v,w))if cost>C:print(-1)exit()ans=ppT.build(0)for p,u,v,w in B:#print(pp,p)#print(cost+w-T.query(u,v))if cost+w-T.query(u,v)<=C:ans=max(ans,max(p,pp))print(ans)