QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#338636 | #5548. Increase the Toll Fees | vwxyz | TL | 901ms | 96520kb | Python3 | 20.5kb | 2024-02-26 05:40:51 | 2024-02-26 05:40:52 |
Judging History
answer
from collections import defaultdict
class Graph:
def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")):
self.V=V
self.directed=directed
self.weighted=weighted
self.inf=inf
if graph!=None:
self.graph=graph
"""
self.edges=[]
for i in range(self.V):
if self.weighted:
for j,d in self.graph[i]:
if self.directed or not self.directed and i<=j:
self.edges.append((i,j,d))
else:
for j in self.graph[i]:
if self.directed or not self.directed and i<=j:
self.edges.append((i,j))
"""
else:
self.edges=edges
self.graph=[[] for i in range(self.V)]
if weighted:
for i,j,d in self.edges:
self.graph[i].append((j,d))
if not self.directed:
self.graph[j].append((i,d))
else:
for i,j in self.edges:
self.graph[i].append(j)
if not self.directed:
self.graph[j].append(i)
def Kruskal(self,maximize=False,spanning_tree=False):
UF=UnionFind(self.V)
sorted_edges=sorted(self.edges if self.weighted else [(x,y,1) for x,y in self.edges],key=lambda tpl:tpl[2],reverse=maximize)
if spanning_tree:
st=[]
else:
cost=0
for x,y,d in sorted_edges:
if not UF.Same(x,y):
UF.Union(x,y)
if spanning_tree:
st.append((x,y,d))
else:
cost+=d
return st if spanning_tree else cost
def SIV_DFS(self,s,bipartite_graph=False,cycle_detection=False,directed_acyclic=False,euler_tour=False,linked_components=False,lowlink=False,parents=False,postorder=False,preorder=False,subtree_size=False,topological_sort=False,unweighted_dist=False,weighted_dist=False):
seen=[False]*self.V
finished=[False]*self.V
if directed_acyclic or cycle_detection or topological_sort:
dag=True
if euler_tour:
et=[]
if linked_components:
lc=[]
if lowlink:
order=[None]*self.V
ll=[None]*self.V
idx=0
if parents or cycle_detection or lowlink or subtree_size:
ps=[None]*self.V
if postorder or topological_sort:
post=[]
if preorder:
pre=[]
if subtree_size:
ss=[1]*self.V
if unweighted_dist or bipartite_graph:
uwd=[self.inf]*self.V
uwd[s]=0
if weighted_dist:
wd=[self.inf]*self.V
wd[s]=0
stack=[(s,0)] if self.weighted else [s]
while stack:
if self.weighted:
x,d=stack.pop()
else:
x=stack.pop()
if not seen[x]:
seen[x]=True
stack.append((x,d) if self.weighted else x)
if euler_tour:
et.append(x)
if linked_components:
lc.append(x)
if lowlink:
order[x]=idx
ll[x]=idx
idx+=1
if preorder:
pre.append(x)
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y]:
stack.append((y,d) if self.weighted else y)
if parents or cycle_detection or lowlink or subtree_size:
ps[y]=x
if unweighted_dist or bipartite_graph:
uwd[y]=uwd[x]+1
if weighted_dist:
wd[y]=wd[x]+d
elif not finished[y]:
if (directed_acyclic or cycle_detection or topological_sort) and dag:
dag=False
if cycle_detection:
cd=(y,x)
elif not finished[x]:
finished[x]=True
if euler_tour:
et.append(~x)
if lowlink:
bl=True
for y in self.graph[x]:
if self.weighted:
y,d=y
if ps[x]==y and bl:
bl=False
continue
ll[x]=min(ll[x],order[y])
if x!=s:
ll[ps[x]]=min(ll[ps[x]],ll[x])
if postorder or topological_sort:
post.append(x)
if subtree_size:
for y in self.graph[x]:
if self.weighted:
y,d=y
if y==ps[x]:
continue
ss[x]+=ss[y]
if bipartite_graph:
bg=[[],[]]
for tpl in self.edges:
x,y=tpl[:2] if self.weighted else tpl
if uwd[x]==self.inf or uwd[y]==self.inf:
continue
if not uwd[x]%2^uwd[y]%2:
bg=False
break
else:
for x in range(self.V):
if uwd[x]==self.inf:
continue
bg[uwd[x]%2].append(x)
retu=()
if bipartite_graph:
retu+=(bg,)
if cycle_detection:
if dag:
cd=[]
else:
y,x=cd
cd=self.Route_Restoration(y,x,ps)
retu+=(cd,)
if directed_acyclic:
retu+=(dag,)
if euler_tour:
retu+=(et,)
if linked_components:
retu+=(lc,)
if lowlink:
retu=(ll,)
if parents:
retu+=(ps,)
if postorder:
retu+=(post,)
if preorder:
retu+=(pre,)
if subtree_size:
retu+=(ss,)
if topological_sort:
if dag:
tp_sort=post[::-1]
else:
tp_sort=[]
retu+=(tp_sort,)
if unweighted_dist:
retu+=(uwd,)
if weighted_dist:
retu+=(wd,)
if len(retu)==1:
retu=retu[0]
return retu
def Build_LCA(self,s,segment_tree=False):
self.lca_segment_tree=segment_tree
if self.lca_segment_tree:
self.lca_euler_tour,self.lca_parents,depth=self.SIV_DFS(s,euler_tour=True,parents=True,unweighted_dist=True)
self.lca_dfs_in_index=[None]*self.V
self.lca_dfs_out_index=[None]*self.V
for i,x in enumerate(self.lca_euler_tour):
if x>=0:
self.lca_dfs_in_index[x]=i
else:
self.lca_dfs_out_index[~x]=i
self.ST=Segment_Tree(2*self.V,lambda x,y:min(x,y),self.V)
lst=[None]*(2*self.V)
for i in range(2*self.V-1):
if self.lca_euler_tour[i]>=0:
lst[i]=depth[self.lca_euler_tour[i]]
else:
lst[i]=depth[self.lca_parents[~self.lca_euler_tour[i]]]
lst[2*self.V-1]=-1
self.ST.Build(lst)
else:
self.lca_parents,self.lca_depth=self.SIV_DFS(s,parents=True,unweighted_dist=True)
self.lca_PD=Path_Doubling(self.V,self.lca_parents)
self.lca_PD.Build_Next(self.V)
def LCA(self,a,b):
if self.lca_segment_tree:
m=min(self.lca_dfs_in_index[a],self.lca_dfs_in_index[b])
M=max(self.lca_dfs_in_index[a],self.lca_dfs_in_index[b])
x=self.lca_euler_tour[self.ST.Fold_Index(m,M+1)]
if x>=0:
lca=x
else:
lca=self.lca_parents[~x]
else:
if self.lca_depth[a]>self.lca_depth[b]:
a,b=b,a
b=self.lca_PD.Permutation_Doubling(b,self.lca_depth[b]-self.lca_depth[a])
if a!=b:
for k in range(self.lca_PD.k-1,-1,-1):
if self.lca_PD.permutation_doubling[k][a]!=self.lca_PD.permutation_doubling[k][b]:
a,b=self.lca_PD.permutation_doubling[k][a],self.lca_PD.permutation_doubling[k][b]
a,b=self.lca_PD.permutation_doubling[0][a],self.lca_PD.permutation_doubling[0][b]
lca=a
return lca
class Segment_Tree:
def __init__(self,N,f,e,lst=None,dynamic=False):
self.f=f
self.e=e
self.N=N
if dynamic:
self.segment_tree=defaultdict(lambda:self.e)
else:
if lst==None:
self.segment_tree=[self.e]*2*self.N
else:
assert len(lst)<=self.N
self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst))
for i in range(self.N-1,0,-1):
self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])
def __getitem__(self,i):
if type(i)==int:
if -self.N<=i<0:
return self.segment_tree[i+self.N*2]
elif 0<=i<self.N:
return self.segment_tree[i+self.N]
else:
raise IndexError("list index out of range")
else:
a,b,c=i.start,i.stop,i.step
if a==None:
a=self.N
else:
a+=self.N
if b==None:
b=self.N*2
else:
b+=self.N
return self.segment_tree[slice(a,b,c)]
def __setitem__(self,i,x):
if -self.N<=i<0:
i+=self.N*2
elif 0<=i<self.N:
i+=self.N
else:
raise IndexError("list index out of range")
self.segment_tree[i]=x
while i>1:
i>>= 1
self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])
def Build(self,lst):
for i,x in enumerate(lst,self.N):
self.segment_tree[i]=x
for i in range(self.N-1,0,-1):
self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])
def Fold(self,L=None,R=None):
if L==None:
L=self.N
else:
L+=self.N
if R==None:
R=self.N*2
else:
R+=self.N
vL=self.e
vR=self.e
while L<R:
if L&1:
vL=self.f(vL,self.segment_tree[L])
L+=1
if R&1:
R-=1
vR=self.f(self.segment_tree[R],vR)
L>>=1
R>>=1
return self.f(vL,vR)
def Fold_Index(self,L=None,R=None):
if L==None:
L=self.N
else:
L+=self.N
if R==None:
R=self.N*2
else:
R+=self.N
if L==R:
return None
x=self.Fold(L-self.N,R-self.N)
while L<R:
if L&1:
if self.segment_tree[L]==x:
i=L
break
L+=1
if R&1:
R-=1
if self.segment_tree[R]==x:
i=R
break
L>>=1
R>>=1
while i<self.N:
if self.segment_tree[i]==self.segment_tree[i<<1]:
i<<=1
else:
i<<=1
i|=1
i-=self.N
return i
def Bisect_Right(self,L=None,f=None):
if L==self.N:
return self.N
if L==None:
L=0
L+=self.N
vl=self.e
vr=self.e
l,r=L,self.N*2
while l<r:
if l&1:
vl=self.f(vl,self.segment_tree[l])
l+=1
if r&1:
r-=1
vr=self.f(self.segment_tree[r],vr)
l>>=1
r>>=1
if f(self.f(vl,vr)):
return self.N
v=self.e
while True:
while L%2==0:
L>>=1
vv=self.f(v,self.segment_tree[L])
if f(vv):
v=vv
L+=1
else:
while L<self.N:
L<<=1
vv=self.f(v,self.segment_tree[L])
if f(vv):
v=vv
L+=1
return L-self.N
def Bisect_Left(self,R=None,f=None):
if R==0:
return 0
if R==None:
R=self.N
R+=self.N
vl=self.e
vr=self.e
l,r=self.N,R
while l<r:
if l&1:
vl=self.f(vl,self.segment_tree[l])
l+=1
if r&1:
r-=1
vr=self.f(self.segment_tree[r],vr)
l>>=1
r>>=1
if f(self.f(vl,vr)):
return 0
v=self.e
while True:
R-=1
while R>1 and R%2:
R>>=1
vv=self.f(self.segment_tree[R],v)
if f(vv):
v=vv
else:
while R<self.N:
R=2*R+1
vv=self.f(self.segment_tree[R],v)
if f(vv):
v=vv
R-=1
return R+1-self.N
def __str__(self):
return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]"
class UnionFind:
def __init__(self,N,label=None,f=None,weighted=False,rollback=False):
self.N=N
self.parents=[None]*self.N
self.size=[1]*self.N
self.roots={i for i in range(self.N)}
self.label=label
if self.label!=None:
self.label=[x for x in label]
self.f=f
self.weighted=weighted
if self.weighted:
self.weight=[0]*self.N
self.rollback=rollback
if self.rollback:
self.operate_list=[]
self.operate_set=[]
def Find(self,x):
stack=[]
while self.parents[x]!=None:
stack.append(x)
x=self.parents[x]
if not self.rollback:
if self.weighted:
w=0
for y in stack[::-1]:
self.parents[y]=x
w+=self.weight[y]
self.weight[y]=w
else:
for y in stack[::-1]:
self.parents[y]=x
return x
def Union(self,x,y,w=None):
root_x=self.Find(x)
root_y=self.Find(y)
if self.rollback:
self.operate_list.append([])
self.operate_set.append([])
if root_x==root_y:
if self.weighted:
if self.weight[y]-self.weight[x]==w:
return True
else:
return False
else:
if self.size[root_x]<self.size[root_y]:
x,y=y,x
root_x,root_y=root_y,root_x
if self.weighted:
w=-w
if self.rollback:
self.operate_list[-1].append((self.parents,root_y,self.parents[root_y]))
self.operate_list[-1].append((self.size,root_x,self.size[root_x]))
self.operate_set[-1].append(root_y)
if self.label!=None:
self.operate_list[-1]((self.label,root_x,self.label[root_x]))
if self.weighted:
self.operate_list[-1].append((self.weight,root_y,self.weight[root_y]))
self.parents[root_y]=root_x
self.size[root_x]+=self.size[root_y]
self.roots.remove(root_y)
if self.label!=None:
self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
if self.weighted:
self.weight[root_y]=w+self.weight[x]-self.weight[y]
def Size(self,x):
return self.size[self.Find(x)]
def Same(self,x,y):
return self.Find(x)==self.Find(y)
def Label(self,x):
return self.label[self.Find(x)]
def Weight(self,x,y):
root_x=self.Find(x)
root_y=self.Find(y)
if root_x!=root_y:
return None
return self.weight[y]-self.weight[x]
def Roots(self):
return list(self.roots)
def Linked_Components_Count(self):
return len(self.roots)
def Linked_Components(self):
linked_components=defaultdict(list)
for x in range(self.N):
linked_components[self.Find(x)].append(x)
return linked_components
def Rollback(self):
assert self.rollback
if self.operate_list:
for lst,x,v in self.operate_list.pop():
lst[x]=v
for x in self.operate_set.pop():
self.roots.add(x)
return True
else:
return False
def __str__(self):
linked_components=defaultdict(list)
for x in range(self.N):
linked_components[self.Find(x)].append(x)
return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys())))
class Path_Doubling:
def __init__(self,N,permutation,lst=None,f=None,e=None):
self.N=N
self.permutation=permutation
self.lst=lst
self.f=f
self.e=e
def Build_Next(self,K=None):
if K==None:
K=self.N
self.k=K.bit_length()
self.permutation_doubling=[[None]*self.N for k in range(self.k)]
for n in range(self.N):
self.permutation_doubling[0][n]=self.permutation[n]
if self.lst!=None:
self.doubling=[[self.e]*self.N for k in range(self.k)]
for n in range(self.N):
self.doubling[0][n]=self.lst[n]
for k in range(1,self.k):
for n in range(self.N):
if self.permutation_doubling[k-1][n]!=None:
self.permutation_doubling[k][n]=self.permutation_doubling[k-1][self.permutation_doubling[k-1][n]]
if self.f!=None:
self.doubling[k][n]=self.f(self.doubling[k-1][n],self.doubling[k-1][self.permutation_doubling[k-1][n]])
def Permutation_Doubling(self,N,K):
if K<0 or 1<<self.k<=K:
return None
for k in range(self.k):
if K>>k&1 and N!=None:
N=self.permutation_doubling[k][N]
return N
def Doubling(self,N,K):
if K<0:
return self.e
retu=self.e
for k in range(self.k):
if K>>k&1:
if self.permutation_doubling[k][N]==None:
return None
retu=self.f(retu,self.doubling[k][N])
N=self.permutation_doubling[k][N]
return N,retu
def Bisect(self,x,is_ok):
if not is_ok(x):
return -1,None
K=0
for k in range(self.k-1,-1,-1):
if is_ok(self.permutation_doubling[k][x]):
K|=1<<k
x=self.permutation_doubling[k][x]
return K,x
N,M=map(int,input().split())
edges=[]
for m in range(M):
u,v,w=map(int,input().split())
u-=1;v-=1
edges.append((u,v,w))
T0=Graph(N,edges=edges,weighted=True).Kruskal(spanning_tree=True)
set_T0=set(T0)
T1=Graph(N,edges=[(u,v,w) for u,v,w in edges if not(u,v,w) in set_T0],weighted=True).Kruskal(spanning_tree=True)
if len(T1)<N-1:
ans=-1
else:
T1=Graph(N,edges=T1,weighted=True)
T1.Build_LCA(0)
depth=T1.lca_depth
parents=[0]+T1.lca_parents[1:]
inf=1<<30
lst=[-inf]*N
for u in range(N):
for v,w in T1.graph[u]:
if v==parents[u]:
continue
lst[v]=w
PD=Path_Doubling(N,permutation=parents,lst=lst,f=max,e=-inf)
PD.Build_Next()
ans=0
for u,v,w in T0:
lca=T1.LCA(u,v)
ma=max(PD.Doubling(u,depth[u]-depth[lca])[1],PD.Doubling(v,depth[v]-depth[lca])[1])
ans+=ma+1-w
print(ans)
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 10244kb
input:
4 6 1 2 2 1 3 5 1 4 5 2 3 3 2 4 5 3 4 4
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 12ms
memory: 10168kb
input:
3 4 1 2 3 2 3 4 1 3 5 1 3 10
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 6ms
memory: 10156kb
input:
5 10 1 2 14 1 3 14 1 4 9 1 5 15 2 3 8 2 3 10 2 4 13 3 4 8 4 5 10 4 5 15
output:
21
result:
ok single line: '21'
Test #4:
score: 0
Accepted
time: 6ms
memory: 10384kb
input:
2 1 1 2 1
output:
-1
result:
ok single line: '-1'
Test #5:
score: 0
Accepted
time: 756ms
memory: 62472kb
input:
29171 100000 7223 21138 270743473 5598 27967 847631606 12666 26050 847631606 75 15747 270743473 8522 12955 847631606 6069 23750 270743473 18708 22605 847631606 16814 22169 847631606 11550 27119 847631606 562 15959 847631606 9400 11110 270743473 15325 23805 270743473 19169 24404 270743473 6649 12062 ...
output:
16827826868780
result:
ok single line: '16827826868780'
Test #6:
score: 0
Accepted
time: 901ms
memory: 96520kb
input:
47977 200000 10970 47321 440845807 1166 29708 767952745 319 37520 546280762 17581 29425 558321466 22079 26884 344816304 7479 44260 791002634 14685 44163 837529020 1537 10359 330017953 8634 27064 969738917 32950 37192 728271930 34751 42782 63025978 32540 34226 86057211 36786 46050 826927874 30444 436...
output:
-1
result:
ok single line: '-1'
Test #7:
score: 0
Accepted
time: 712ms
memory: 46248kb
input:
28825 57648 9446 22014 286256842 14902 20222 14175 3246 20218 80493268 1783 13768 931622563 11107 24862 918832025 25070 27312 98899079 8535 20222 16037 9184 17491 294248461 8799 17834 456827944 1152 11687 960740527 17849 23045 9706 5774 21436 444202963 5417 23045 3092 20222 20370 11232 16585 20222 1...
output:
28822649262260
result:
ok single line: '28822649262260'
Test #8:
score: -100
Time Limit Exceeded
input:
22253 200000 46 10310 674985053 2403 19582 788128238 5203 7897 985236456 2709 9034 824557880 14337 20524 230824854 19901 22177 99959362 5067 8568 570267383 13707 21474 610729058 7494 7860 109319713 14473 16182 794578653 21 17055 852110939 19320 21640 844993191 2443 21673 170358534 5941 9705 16465049...
output:
3270266304988